CS236781: Deep Learning on Computational Accelerators¶

Homework Assignment 2¶

Faculty of Computer Science, Technion.

Submitted by:

# Name Id email
Student 1 Ofer Nissim 312367576 ofer.nissim@campus.technion.ac.il
Student 2 Lotan Amit 208800342 lotan.amit@campus.technion.ac.il

Introduction¶

In this assignment we'll create a from-scratch implementation of two fundemental deep learning concepts: the backpropagation algorithm and stochastic gradient descent-based optimizers. In addition, you will create a general-purpose multilayer perceptron, the core building block of deep neural networks. We'll visualize decision bounrdaries and ROC curves in the context of binary classification. Following that we will focus on convolutional networks with residual blocks. We'll create our own network architectures and train them using GPUs on the course servers, then we'll conduct architecture experiments to determine the the effects of different architectural decisions on the performance of deep networks.

General Guidelines¶

  • Please read the getting started page on the course website. It explains how to setup, run and submit the assignment.
  • Please read the course servers usage guide. It explains how to use and run your code on the course servers to benefit from training with GPUs.
  • The text and code cells in these notebooks are intended to guide you through the assignment and help you verify your solutions. The notebooks do not need to be edited at all (unless you wish to play around). The only exception is to fill your name(s) in the above cell before submission. Please do not remove sections or change the order of any cells.
  • All your code (and even answers to questions) should be written in the files within the python package corresponding the assignment number (hw1, hw2, etc). You can of course use any editor or IDE to work on these files.

Contents¶

  • Part 1: Backpropagation
  • Part 2: Optimization and Training:
  • Part 3: Binary Classification with Multilayer Perceptrons
  • Part 4: Convolutional Neural Networks:
  • Part 5: Convolutional Architecture Experiments
  • Part 6: YOLO - Object Detection
$$ \newcommand{\mat}[1]{\boldsymbol {#1}} \newcommand{\mattr}[1]{\boldsymbol {#1}^\top} \newcommand{\matinv}[1]{\boldsymbol {#1}^{-1}} \renewcommand{\vec}[1]{\boldsymbol {#1}} \newcommand{\vectr}[1]{\boldsymbol {#1}^\top} \newcommand{\rvar}[1]{\mathrm {#1}} \newcommand{\rvec}[1]{\boldsymbol{\mathrm{#1}}} \newcommand{\diag}{\mathop{\mathrm {diag}}} \renewcommand{\set}[1]{\mathbb {#1}} \newcommand{\norm}[1]{\left\lVert#1\right\rVert} \newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}} \newcommand{\bb}[1]{\boldsymbol{#1}} $$

Part 1: Backpropagation¶

In this part, we'll implement backpropagation and automatic differentiation from scratch and compare our implementations to PyTorch's built in implementation (autograd).

In [1]:
import torch
import unittest

%load_ext autoreload
%autoreload 2

test = unittest.TestCase()

Reminder: The backpropagation algorithm is at the core of training deep models. To state the problem we'll tackle in this notebook, imagine we have an L-layer MLP model, defined as $$ \hat{\vec{y}^i} = \vec{y}L^i= \varphi_L \left( \mat{W}_L \varphi{L-1} \left( \cdots \varphi_1 \left( \mat{W}_1 \vec{x}^i + \vec{b}_1 \right) \cdots \right)

  • \vec{b}_L \right),
$$ a pointwise loss function $\ell(\vec{y}, \hat{\vec{y}})$ and an empirical loss over our entire data set, $$

L(\vec{\theta}) = \frac{1}{N} \sum_{i=1}^{N} \ell(\vec{y}^i, \hat{\vec{y}^i}) + R(\vec{\theta}) $$

where $\vec{\theta}$ is a vector containing all network parameters, e.g. $\vec{\theta} = \left[ \mat{W}_{1,:}, \vec{b}_1, \dots, \mat{W}_{L,:}, \vec{b}_L \right]$.

In order to train our model we would like to calculate the derivative (or gradient, in the multivariate case) of the loss with respect to each and every one of the parameters, i.e. $\pderiv{L}{\mat{W}_j}$ and $\pderiv{L}{\vec{b}_j}$ for all $j$. Since the gradient "points" to the direction of functional increase, the negative gradient is often used as a descent direction for descent-based optimization algorithms. In other words, iteratively updating each parameter proportianally to it's negetive gradient can lead to convergence to a local minimum of the loss function.

Calculus tells us that as long as we know the derivatives of all the functions "along the way" ($\varphi_i(\cdot),\ \ell(\cdot,\cdot),\ R(\cdot)$) we can use the chain rule to calculate the derivative of the loss with respect to any one of the parameter vectors. Note that if the loss $L(\vec{\theta})$ is scalar (which is usually the case), the gradient of a parameter will have the same shape as the parameter itself (matrix/vector/tensor of same dimensions).

For deep models that are a composition of many functions, calculating the gradient of each parameter by hand and implementing hard-coded gradient derivations quickly becomes infeasible. Additionally, such code makes models hard to change, since any change potentially requires re-derivation and re-implementation of the entire gradient function.

The backpropagation algorithm, which we saw in the lecture, provides us with a effective method of applying the chain rule recursively so that we can implement gradient calculations of arbitrarily deep or complex models.

We'll now implement backpropagation using a modular approach, which will allow us to chain many components layers together and get automatic gradient calculation of the output with respect to the input or any intermediate parameter.

To do this, we'll define a Layer class. Here's the API of this class:

In [2]:
import hw2.layers as layers
help(layers.Layer)
Help on class Layer in module hw2.layers:

class Layer(abc.ABC)
 |  A Layer is some computation element in a network architecture which
 |  supports automatic differentiation using forward and backward functions.
 |  
 |  Method resolution order:
 |      Layer
 |      abc.ABC
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __call__(self, *args, **kwargs)
 |      Call self as a function.
 |  
 |  __init__(self)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __repr__(self)
 |      Return repr(self).
 |  
 |  backward(self, dout)
 |      Computes the backward pass of the layer, i.e. the gradient
 |      calculation of the final network output with respect to each of the
 |      parameters of the forward function.
 |      :param dout: The gradient of the network with respect to the
 |      output of this layer.
 |      :return: A tuple with the same number of elements as the parameters of
 |      the forward function. Each element will be the gradient of the
 |      network output with respect to that parameter.
 |  
 |  forward(self, *args, **kwargs)
 |      Computes the forward pass of the layer.
 |      :param args: The computation arguments (implementation specific).
 |      :return: The result of the computation.
 |  
 |  params(self)
 |      :return: Layer's trainable parameters and their gradients as a list
 |      of tuples, each tuple containing a tensor and it's corresponding
 |      gradient tensor.
 |  
 |  train(self, training_mode=True)
 |      Changes the mode of this layer between training and evaluation (test)
 |      mode. Some layers have different behaviour depending on mode.
 |      :param training_mode: True: set the model in training mode. False: set
 |      evaluation mode.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __abstractmethods__ = frozenset({'backward', 'forward', 'params'})

In other words, a Layer can be anything: a layer, an activation function, a loss function or generally any computation that we know how to derive a gradient for.

Each Layer must define a forward() function and a backward() function.

  • The forward() function performs the actual calculation/operation of the block and returns an output.
  • The backward() function computes the gradient of the input and parameters as a function of the gradient of the output, according to the chain rule.

Here's a diagram illustrating the above explanation:

Note that the diagram doesn't show that if the function is parametrized, i.e. $f(\vec{x},\vec{y})=f(\vec{x},\vec{y};\vec{w})$, there are also gradients to calculate for the parameters $\vec{w}$.

The forward pass is straightforward: just do the computation. To understand the backward pass, imagine that there's some "downstream" loss function $L(\vec{\theta})$ and magically somehow we are told the gradient of that loss with respect to the output $\vec{z}$ of our block, i.e. $\pderiv{L}{\vec{z}}$.

Now, since we know how to calculate the derivative of $f(\vec{x},\vec{y};\vec{w})$, it means we know how to calculate $\pderiv{\vec{z}}{\vec{x}}$, $\pderiv{\vec{z}}{\vec{y}}$ and $\pderiv{\vec{z}}{\vec{w}}$ . Thanks to the chain rule, this is all we need to calculate the gradients of the loss w.r.t. the input and parameters:

$$ \begin{align} \pderiv{L}{\vec{x}} &= \pderiv{L}{\vec{z}}\cdot \pderiv{\vec{z}}{\vec{x}}\\ \pderiv{L}{\vec{y}} &= \pderiv{L}{\vec{z}}\cdot \pderiv{\vec{z}}{\vec{y}}\\ \pderiv{L}{\vec{w}} &= \pderiv{L}{\vec{z}}\cdot \pderiv{\vec{z}}{\vec{w}} \end{align} $$

Comparison with PyTorch¶

PyTorch has the nn.Module base class, which may seem to be similar to our Layer since it also represents a computation element in a network. However PyTorch's nn.Modules don't compute the gradient directly, they only define the forward calculations. Instead, PyTorch has a more low-level API for defining a function and explicitly implementing it's forward() and backward(). See autograd.Function. When an operation is performed on a tensor, it creates a Function instance which performs the operation and stores any necessary information for calculating the gradient later on. Additionally, Functionss point to the other Function objects representing the operations performed earlier on the tensor. Thus, a graph (or DAG) of operations is created (this is not 100% exact, as the graph is actually composed of a different type of class which wraps the backward method, but it's accurate enough for our purposes).

A Tensor instance which was created by performing operations on one or more tensors with requires_grad=True, has a grad_fn property which is a Function instance representing the last operation performed to produce this tensor. This exposes the graph of Function instances, each with it's own backward() function. Therefore, in PyTorch the backward() function is called on the tensors, not the modules.

Our Layers are therefore a combination of the ideas in Module and Function and we'll implement them together, just to make things simpler. Our goal here is to create a "poor man's autograd": We'll use PyTorch tensors, but we'll calculate and store the gradients in our Layers (or return them). The gradients we'll calculate are of the entire block, not individual operations on tensors.

To test our implementation, we'll use PyTorch's autograd.

Note that of course this method of tracking gradients is much more limited than what PyTorch offers. However it allows us to implement the backpropagation algorithm very simply and really see how it works.

Let's set up some testing instrumentation:

In [3]:
from hw2.grad_compare import compare_layer_to_torch

def test_block_grad(block: layers.Layer, x, y=None, delta=1e-3):
    diffs = compare_layer_to_torch(block, x, y)
    
    # Assert diff values
    for diff in diffs:
        test.assertLess(diff, delta)

# Show the compare function
compare_layer_to_torch??

Notes:

  • After you complete your implementation, you should make sure to read and understand the compare_layer_to_torch() function. It will help you understand what PyTorch is doing.
  • The value of delta above is should not be needed. A correct implementation will give you a diff of exactly zero.

Layer Implementations¶

We'll now implement some Layers that will enable us to later build an MLP model of arbitrary depth, complete with automatic differentiation.

For each block, you'll first implement the forward() function. Then, you will calculate the derivative of the block by hand with respect to each of its input tensors and each of its parameter tensors (if any). Using your manually-calculated derivation, you can then implement the backward() function.

Notice that we have intermediate Jacobians that are potentially high dimensional tensors. For example in the expression $\pderiv{L}{\vec{w}} = \pderiv{L}{\vec{z}}\cdot \pderiv{\vec{z}}{\vec{w}}$, the term $\pderiv{\vec{z}}{\vec{w}}$ is a 4D Jacobian if both $\vec{z}$ and $\vec{w}$ are 2D matrices.

In order to implement the backpropagation algorithm efficiently, we need to implement every backward function without explicitly constructing this Jacobian. Instead, we're interested in directly calculating the vector-Jacobian product (VJP) $\pderiv{L}{\vec{z}}\cdot \pderiv{\vec{z}}{\vec{w}}$. In order to do this, you should try to figure out the gradient of the loss with respect to one element, e.g. $\pderiv{L}{\vec{w}_{1,1}}$ and extrapolate from there how to directly obtain the VJP.

Activation functions¶

(Leaky) ReLU¶

ReLU, or rectified linear unit is a very common activation function in deep learning architectures. In it's most standard form, as we'll implement here, it has no parameters.

We'll first implement the "leaky" version, defined as

$$ \mathrm{relu}(\vec{x}) = \max(\alpha\vec{x},\vec{x}), \ 0\leq\alpha<1 $$

This is similar to the ReLU activation we've seen in class, only that it has a small non-zero slope then it's input is negative. Note that it's not strictly differentiable, however it has sub-gradients, defined separately any positive-valued input and for negative-valued input.

TODO: Complete the implementation of the LeakyReLU class in the hw2/layers.py module.

In [4]:
N = 100
in_features = 200
num_classes = 10
eps = 1e-6
In [5]:
# Test LeakyReLU
alpha = 0.1
lrelu = layers.LeakyReLU(alpha=alpha)
x_test = torch.randn(N, in_features)

# Test forward pass
z = lrelu(x_test)
test.assertSequenceEqual(z.shape, x_test.shape)
test.assertTrue(torch.allclose(z, torch.nn.LeakyReLU(alpha)(x_test), atol=eps))

# Test backward pass
test_block_grad(lrelu, x_test)
Comparing gradients... 
input    diff=0.000

Now using the LeakyReLU, we can trivially define a regular ReLU block as a special case.

TODO: Complete the implementation of the ReLU class in the hw2/layers.py module.

In [6]:
# Test ReLU
relu = layers.ReLU()
x_test = torch.randn(N, in_features)

# Test forward pass
z = relu(x_test)
test.assertSequenceEqual(z.shape, x_test.shape)
test.assertTrue(torch.allclose(z, torch.relu(x_test), atol=eps))

# Test backward pass
test_block_grad(relu, x_test)
Comparing gradients... 
input    diff=0.000

Sigmoid¶

The sigmoid function $\sigma(x)$ is also sometimes used as an activation function. We have also seen it previously in the context of logistic regression.

The sigmoid function is defined as

$$ \sigma(\vec{x}) = \frac{1}{1+\exp(-\vec{x})}. $$
In [7]:
# Test Sigmoid
sigmoid = layers.Sigmoid()
x_test = torch.randn(N, in_features, in_features) # 3D input should work

# Test forward pass
z = sigmoid(x_test)
test.assertSequenceEqual(z.shape, x_test.shape)
test.assertTrue(torch.allclose(z, torch.sigmoid(x_test), atol=eps))

# Test backward pass
test_block_grad(sigmoid, x_test)
Comparing gradients... 
input    diff=0.000

Hyperbolic Tangent¶

The hyperbolic tangent function $\tanh(x)$ is a common activation function used when the output should be in the range [-1, 1].

The tanh function is defined as

$$ \tanh(\vec{x}) = \frac{\exp(x)-\exp(-x)}{\exp(x)+\exp(-\vec{x})}. $$
In [8]:
# Test TanH
tanh = layers.TanH()
x_test = torch.randn(N, in_features, in_features) # 3D input should work

# Test forward pass
z = tanh(x_test)
test.assertSequenceEqual(z.shape, x_test.shape)
test.assertTrue(torch.allclose(z, torch.tanh(x_test), atol=eps))

# Test backward pass
test_block_grad(tanh, x_test)
Comparing gradients... 
input    diff=0.000

Linear (fully connected) layer¶

First, we'll implement an affine transform layer, also known as a fully connected layer.

Given an input $\mat{X}$ the layer computes,

$$ \mat{Z} = \mat{X} \mattr{W} + \vec{b} ,~ \mat{X}\in\set{R}^{N\times D_{\mathrm{in}}},~ \mat{W}\in\set{R}^{D_{\mathrm{out}}\times D_{\mathrm{in}}},~ \vec{b}\in\set{R}^{D_{\mathrm{out}}}. $$

Notes:

  • We write it this way to follow the implementation conventions.
  • $N$ is the number of samples in the input (batch size). The input $\mat{X}$ will always be a tensor containing a batch dimension first.
  • Thanks to broadcasting, $\vec{b}$ can remain a vector even though the input $\mat{X}$ is a matrix.

TODO: Complete the implementation of the Linear class in the hw2/layers.py module.

In [9]:
# Test Linear
out_features = 1000
fc = layers.Linear(in_features, out_features)
x_test = torch.randn(N, in_features)

# Test forward pass
z = fc(x_test)
test.assertSequenceEqual(z.shape, [N, out_features])
torch_fc = torch.nn.Linear(in_features, out_features,bias=True)
torch_fc.weight = torch.nn.Parameter(fc.w)
torch_fc.bias = torch.nn.Parameter(fc.b)
test.assertTrue(torch.allclose(torch_fc(x_test), z, atol=eps))

# Test backward pass
test_block_grad(fc, x_test)

# Test second backward pass
x_test = torch.randn(N, in_features)
z = fc(x_test)
z = fc(x_test)
test_block_grad(fc, x_test)
Comparing gradients... 
input    diff=0.000
param#01 diff=0.000
param#02 diff=0.000
Comparing gradients... 
input    diff=0.000
param#01 diff=0.000
param#02 diff=0.000

Cross-Entropy Loss¶

As you know by know, cross-entropy is a common loss function for classification tasks. In class, we defined it as

$$\ell_{\mathrm{CE}}(\vec{y},\hat{\vec{y}}) = - {\vectr{y}} \log(\hat{\vec{y}})$$

where $\hat{\vec{y}} = \mathrm{softmax}(x)$ is a probability vector (the output of softmax on the class scores $\vec{x}$) and the vector $\vec{y}$ is a 1-hot encoded class label.

However, it's tricky to compute the gradient of softmax, so instead we'll define a version of cross-entropy that produces the exact same output but works directly on the class scores $\vec{x}$.

We can write, $$\begin{align} \ell_{\mathrm{CE}}(\vec{y},\hat{\vec{y}}) &= - {\vectr{y}} \log(\hat{\vec{y}}) = - {\vectr{y}} \log\left(\mathrm{softmax}(\vec{x})\right) \\ &= - {\vectr{y}} \log\left(\frac{e^{\vec{x}}}{\sum_k e^{x_k}}\right) \\ &= - \log\left(\frac{e^{x_y}}{\sum_k e^{x_k}}\right) \\ &= - \left(\log\left(e^{x_y}\right) - \log\left(\sum_k e^{x_k}\right)\right)\\ &= - x_y + \log\left(\sum_k e^{x_k}\right) \end{align}$$

Where the scalar $y$ is the correct class label, so $x_y$ is the correct class score.

Note that this version of cross entropy is also what's provided by PyTorch's nn module.

TODO: Complete the implementation of the CrossEntropyLoss class in the hw2/layers.py module.

In [10]:
# Test CrossEntropy
cross_entropy = layers.CrossEntropyLoss()
scores = torch.randn(N, num_classes)
labels = torch.randint(low=0, high=num_classes, size=(N,), dtype=torch.long)

# Test forward pass
loss = cross_entropy(scores, labels)
expected_loss = torch.nn.functional.cross_entropy(scores, labels)
test.assertLess(torch.abs(expected_loss-loss).item(), 1e-5)
print('loss=', loss.item())

# Test backward pass
test_block_grad(cross_entropy, scores, y=labels)
loss= 2.7283618450164795
Comparing gradients... 
input    diff=0.000

Building Models¶

Now that we have some working Layers, we can build an MLP model of arbitrary depth and compute end-to-end gradients.

First, lets copy an idea from PyTorch and implement our own version of the nn.Sequential Module. This is a Layer which contains other Layers and calls them in sequence. We'll use this to build our MLP model.

TODO: Complete the implementation of the Sequential class in the hw2/layers.py module.

In [11]:
# Test Sequential
# Let's create a long sequence of layers and see
# whether we can compute end-to-end gradients of the whole thing.

seq = layers.Sequential(
    layers.Linear(in_features, 100),
    layers.Linear(100, 200),
    layers.Linear(200, 100),
    layers.ReLU(),
    layers.Linear(100, 500),
    layers.LeakyReLU(alpha=0.01),
    layers.Linear(500, 200),
    layers.ReLU(),
    layers.Linear(200, 500),
    layers.LeakyReLU(alpha=0.1),
    layers.Linear(500, 1),
    layers.Sigmoid(),
)
x_test = torch.randn(N, in_features)

# Test forward pass
z = seq(x_test)
test.assertSequenceEqual(z.shape, [N, 1])

# Test backward pass
test_block_grad(seq, x_test)
Comparing gradients... 
input    diff=0.000
param#01 diff=0.000
param#02 diff=0.000
param#03 diff=0.000
param#04 diff=0.000
param#05 diff=0.000
param#06 diff=0.000
param#07 diff=0.000
param#08 diff=0.000
param#09 diff=0.000
param#10 diff=0.000
param#11 diff=0.000
param#12 diff=0.000
param#13 diff=0.000
param#14 diff=0.000

Now, equipped with a Sequential, all we have to do is create an MLP architecture. We'll define our MLP with the following hyperparameters:

  • Number of input features, $D$.
  • Number of output classes, $C$.
  • Sizes of hidden layers, $h_1,\dots,h_L$.

So the architecture will be:

FC($D$, $h_1$) $\rightarrow$ ReLU $\rightarrow$ FC($h_1$, $h_2$) $\rightarrow$ ReLU $\rightarrow$ $\cdots$ $\rightarrow$ FC($h_{L-1}$, $h_L$) $\rightarrow$ ReLU $\rightarrow$ FC($h_{L}$, $C$)

We'll also create a sequence of the above MLP and a cross-entropy loss, since it's the gradient of the loss that we need in order to train a model.

TODO: Complete the implementation of the MLP class in the hw2/layers.py module. Ignore the dropout parameter for now.

In [12]:
# Create an MLP model
mlp = layers.MLP(in_features, num_classes, hidden_features=[100, 50, 100])
print(mlp)
MLP, Sequential
	[0] Linear(self.in_features=200, self.out_features=100)
	[1] ReLU
	[2] Linear(self.in_features=100, self.out_features=50)
	[3] ReLU
	[4] Linear(self.in_features=50, self.out_features=100)
	[5] ReLU
	[6] Linear(self.in_features=100, self.out_features=10)

In [13]:
# Test MLP architecture
N = 100
in_features = 10
num_classes = 10
for activation in ('relu', 'sigmoid'):
    mlp = layers.MLP(in_features, num_classes, hidden_features=[100, 50, 100], activation=activation)
    test.assertEqual(len(mlp.sequence), 7)
    
    num_linear = 0
    for b1, b2 in zip(mlp.sequence, mlp.sequence[1:]):
        if (str(b2).lower() == activation):
            test.assertTrue(str(b1).startswith('Linear'))
            num_linear += 1
            
    test.assertTrue(str(mlp.sequence[-1]).startswith('Linear'))
    test.assertEqual(num_linear, 3)

    # Test MLP gradients
    # Test forward pass
    x_test = torch.randn(N, in_features)
    labels = torch.randint(low=0, high=num_classes, size=(N,), dtype=torch.long)
    z = mlp(x_test)
    test.assertSequenceEqual(z.shape, [N, num_classes])

    # Create a sequence of MLPs and CE loss
    seq_mlp = layers.Sequential(mlp, layers.CrossEntropyLoss())
    loss = seq_mlp(x_test, y=labels)
    test.assertEqual(loss.dim(), 0)
    print(f'MLP loss={loss}, activation={activation}')

    # Test backward pass
    test_block_grad(seq_mlp, x_test, y=labels)
MLP loss=2.30924391746521, activation=relu
Comparing gradients... 
input    diff=0.000
param#01 diff=0.000
param#02 diff=0.000
param#03 diff=0.000
param#04 diff=0.000
param#05 diff=0.000
param#06 diff=0.000
param#07 diff=0.000
param#08 diff=0.000
MLP loss=2.3934404850006104, activation=sigmoid
Comparing gradients... 
input    diff=0.000
param#01 diff=0.000
param#02 diff=0.000
param#03 diff=0.000
param#04 diff=0.000
param#05 diff=0.000
param#06 diff=0.000
param#07 diff=0.000
param#08 diff=0.000

If the above tests passed then congratulations - you've now implemented an arbitrarily deep model and loss function with end-to-end automatic differentiation!

Questions¶

TODO Answer the following questions. Write your answers in the appropriate variables in the module hw2/answers.py.

In [14]:
from cs236781.answers import display_answer
import hw2.answers

Question 1¶

Suppose we have a linear (i.e. fully-connected) layer with a weight tensor $\mat{W}$, defined with in_features=1024 and out_features=512. We apply this layer to an input tensor $\mat{X}$ containing a batch of N=64 samples. The output of the layer is denoted as $\mat{Y}$.

  1. Consider the Jacobian tensor $\pderiv{\mat{Y}}{\mat{X}}$ of the output of the layer w.r.t. the input $\mat{X}$.

    1. What is the shape of this tensor?
    2. Is this Jacobian sparse (most elements zero by definition)? If so, why and which elements?
    3. Given the gradient of the output w.r.t. some downstream scalar loss $L$, $\delta\mat{Y}=\pderiv{L}{\mat{Y}}$, do we need to materialize the above Jacobian in order to calculate the downstream gratdient w.r.t. to the input ($\delta\mat{X}$)? If yes, explain why; if no, show how to calcualte it without materializing the Jacobian.
  2. Consider the Jacobian tensor $\pderiv{\mat{Y}}{\mat{W}}$ of the output of the layer w.r.t. the layer weights $\mat{W}$. Answer questions A-C about it as well.

In [15]:
display_answer(hw2.answers.part1_q1)

Your answer:

  1. For the Jacobian tensor $\pderiv{\mat{Y}}{\mat{X}}$:

    1. The shape of this tensor will be (64, 512, 64, 1024).
    2. This Jacobian is sparse. We're differentiating each output sample (in Y) w.r.t each input sample (in X) and we know

    that each output sample derivative depends only on its corresponding input sample. Hence, the only elements that are not zero correspond to indices of the form (i,j,i,k) where $1<=i<=64$, meaning that this is the derivative of the i-th output sample w.r.t its corresponding input sample (same i-th index). 3. No, we don't need to materialize the Jacobian tensor $\pderiv{\mat{Y}}{\mat{X}}$ in order to calculate the downstream gradient $\delta\mat{X}$

     Instead, we can leverage the chain rule to compute the gradient efficiently:
     $$
     \delta\mat{X}=\pderiv{L}{\mat{X}} = \pderiv{L}{\mat{Y}} \pderiv{\mat{Y}}{\mat{X}}
     $$
     - $\pderiv{L}{\mat{Y}}$ is given to us.
     - $\pderiv{\mat{Y}}{\mat{X}}$ is $\mattr{W}$ (because $\mat{Y}=\mat{X} \mattr{W} + \vec{b}$ - Using matrix differentiation rules).  
    
    

    As we saw in the previous clause, the only non-zero elements of the Jacobian tensor $\pderiv{\mat{Y}}{\mat{X}}$ have the index form of (i,j,i,k), meaning that only those elements in the Jacobian tensor are needed in order to compute the gradient (all other elements in the product matrix will be set to zero).
    So eventually we get: $$ \delta\mat{X}=\pderiv{L}{\mat{Y}}\mattr{W}=\delta\mat{Y}\mattr{W} $$

  2. For the Jacobian tensor $\pderiv{\mat{Y}}{\mat{W}}$:

    1. The shape of this tensor will be (64, 512, 512, 1024).
    2. This Jacobian is sparse again, for the same reason as earlier - We're differentiating each output feature (in Y) w.r.t each input feature (in W),

    such that the only non-zero elements correspond to indices of the form (i,j,i,k) where $1<=i<=64$, meaning that this is the derivative of the i-th output feature w.r.t its corresponding input feature (same i-th index). 3. No, given the gradient of the output $\pderiv{\mat{L}}{\mat{Y}}$, we can again use the chain rule to compute the gradient efficiently: $$ \delta\mat{W}=\pderiv{L}{\mat{W}} = \pderiv{L}{\mat{Y}} \pderiv{\mat{Y}}{\mat{W}} $$ - $\pderiv{L}{\mat{Y}}$ is given to us. - $\pderiv{\mat{Y}}{\mat{W}}$ is $\mattr{X}$ (because $\mat{Y}=\mat{X} \mattr{W} + \vec{b}$ - Using matrix differentiation rules).

    As we saw in the previous clause, the only non-zero elements of the Jacobian tensor $\pderiv{\mat{Y}}{\mat{W}}$ have the index form of (i,j,i,k), meaning that only those elements in the Jacobian tensor are needed in order to compute the gradient (all other elements in the product matrix will be set to zero). So eventually we get: $$ \delta\mat{W}=\pderiv{L}{\mat{Y}}\mattr{W}=\delta\mat{Y}\mattr{X} $$

TODO¶

Question 2¶

Is back-propagation required in order to train neural networks with decent-based optimization? Why or why not?

In [16]:
display_answer(hw2.answers.part1_q2)

Your answer:

Yes, for any practical purposes, backpropagation is required in order to train neural networks.

This is because without backpropagation, it would be difficult and computationally expensive (and even infeasible) to calculate these gradients manually. Backpropagation automates the process, making training feasible and efficient.

$$ \newcommand{\mat}[1]{\boldsymbol {#1}} \newcommand{\mattr}[1]{\boldsymbol {#1}^\top} \newcommand{\matinv}[1]{\boldsymbol {#1}^{-1}} \renewcommand{\vec}[1]{\boldsymbol {#1}} \newcommand{\vectr}[1]{\boldsymbol {#1}^\top} \newcommand{\rvar}[1]{\mathrm {#1}} \newcommand{\rvec}[1]{\boldsymbol{\mathrm{#1}}} \newcommand{\diag}{\mathop{\mathrm {diag}}} \renewcommand{\set}[1]{\mathbb {#1}} \newcommand{\norm}[1]{\left\lVert#1\right\rVert} \newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}} \newcommand{\bb}[1]{\boldsymbol{#1}} $$

Part 2: Optimization and Training¶

In this part we will learn how to implement optimization algorithms for deep networks. Additionally, we'll learn how to write training loops and implement a modular model trainer. We'll use our optimizers and training code to test a few configurations for classifying images with an MLP model.

In [1]:
import os
import numpy as np
import matplotlib.pyplot as plt
import unittest
import torch
import torchvision
import torchvision.transforms as tvtf

%matplotlib inline
%load_ext autoreload
%autoreload 2
In [2]:
seed = 42
plt.rcParams.update({'font.size': 12})
test = unittest.TestCase()

Implementing Optimization Algorithms¶

In the context of deep learning, an optimization algorithm is some method of iteratively updating model parameters so that the loss converges toward some local minimum (which we hope will be good enough).

Gradient descent-based methods are by far the most popular algorithms for optimization of neural network parameters. However the high-dimensional loss-surfaces we encounter in deep learning applications are highly non-convex. They may be riddled with local minima, saddle points, large plateaus and a host of very challenging "terrain" for gradient-based optimization. This gave rise to many different methods of performing the parameter updates based on the loss gradients, aiming to tackle these optimization challenges.

The most basic gradient-based update rule can be written as,

$$ \vec{\theta} \leftarrow \vec{\theta} - \eta \nabla_{\vec{\theta}} L(\vec{\theta}; \mathcal{D}) $$

where $\mathcal{D} = \left\{ (\vec{x}^i, \vec{y}^i) \right\}_{i=1}^{M}$ is our training dataset or part of it. Specifically, if we have in total $N$ training samples, then

  • If $M=N$ this is known as regular gradient descent. If the dataset does not fit in memory the gradient of this loss becomes infeasible to compute.
  • If $M=1$, the loss is computed w.r.t. a single different sample each time. This is known as stochastic gradient descent.
  • If $1<M<N$ this is known as stochastic mini-batch gradient descent. This is the most commonly-used option.

The intuition behind gradient descent is simple: since the gradient of a multivariate function points to the direction of steepest ascent ("uphill"), we move in the opposite direction. A small step size $\eta$ known as the learning rate is required since the gradient can only serve as a first-order linear approximation of the function's behaviour at $\vec{\theta}$ (recall e.g. the Taylor expansion). However in truth our loss surface generally has nontrivial curvature caused by a high order nonlinear dependency on $\vec{\theta}$. Thus taking a large step in the direction of the gradient is actually just as likely to increase the function value.

The idea behind the stochastic versions is that by constantly changing the samples we compute the loss with, we get a dynamic error surface, i.e. it's different for each set of training samples. This is thought to generally improve the optimization since it may help the optimizer get out of flat regions or sharp local minima since these features may disappear in the loss surface of subsequent batches. The image below illustrates this. The different lines are different 1-dimensional losses for different training set-samples.

Deep learning frameworks generally provide implementations of various gradient-based optimization algorithms. Here we'll implement our own optimization module from scratch, this time keeping a similar API to the PyTorch optim package.

We define a base Optimizer class. An optimizer holds a set of parameter tensors (these are the trainable parameters of some model) and maintains internal state. It may be used as follows:

  • After the forward pass has been performed the optimizer's zero_grad() function is invoked to clear the parameter gradients computed by previous iterations.
  • After the backward pass has been performed, and gradients have been calculated for these parameters, the optimizer's step() function is invoked in order to update the value of each parameter based on it's gradient.

The exact method of update is implementation-specific for each optimizer and may depend on its internal state. In addition, adding the regularization penalty to the gradient is handled by the optimizer since it only depends on the parameter values (and not the data).

Here's the API of our Optimizer:

In [3]:
import hw2.optimizers as optimizers
help(optimizers.Optimizer)
Help on class Optimizer in module hw2.optimizers:

class Optimizer(abc.ABC)
 |  Optimizer(params)
 |  
 |  Base class for optimizers.
 |  
 |  Method resolution order:
 |      Optimizer
 |      abc.ABC
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, params)
 |      :param params: A sequence of model parameters to optimize. Can be a
 |      list of (param,grad) tuples as returned by the Layers, or a list of
 |      pytorch tensors in which case the grad will be taken from them.
 |  
 |  step(self)
 |      Updates all the registered parameter values based on their gradients.
 |  
 |  zero_grad(self)
 |      Sets the gradient of the optimized parameters to zero (in place).
 |  
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |  
 |  params
 |      :return: A sequence of parameter tuples, each tuple containing
 |      (param_data, param_grad). The data should be updated in-place
 |      according to the grad.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __abstractmethods__ = frozenset({'step'})

Vanilla SGD with Regularization¶

Let's start by implementing the simplest gradient based optimizer. The update rule will be exacly as stated above, but we'll also add a L2-regularization term to the gradient. Remember that in the loss function, the L2 regularization term is expressed by

$$R(\vec{\theta}) = \frac{1}{2}\lambda||\vec{\theta}||^2_2.$$

TODO: Complete the implementation of the VanillaSGD class in the hw2/optimizers.py module.

In [4]:
# Test VanillaSGD
torch.manual_seed(42)
p = torch.randn(500, 10)
dp = torch.randn(*p.shape)*2
params = [(p, dp)]

vsgd = optimizers.VanillaSGD(params, learn_rate=0.5, reg=0.1)
vsgd.step()

expected_p = torch.load('tests/assets/expected_vsgd.pt')
diff = torch.norm(p-expected_p).item()
print(f'diff={diff}')
test.assertLess(diff, 1e-3)
diff=1.0932822078757454e-06

Training¶

Now that we can build a model and loss function, compute their gradients and we have an optimizer, we can finally do some training!

In the spirit of more modular software design, we'll implement a class that will aid us in automating the repetitive training loop code that we usually write over and over again. This will be useful for both training our Layer-based models and also later for training PyTorch nn.Modules.

Here's our Trainer API:

In [5]:
import hw2.training as training
help(training.Trainer)
Help on class Trainer in module hw2.training:

class Trainer(abc.ABC)
 |  Trainer(model: torch.nn.modules.module.Module, device: Union[torch.device, NoneType] = None)
 |  
 |  A class abstracting the various tasks of training models.
 |  
 |  Provides methods at multiple levels of granularity:
 |  - Multiple epochs (fit)
 |  - Single epoch (train_epoch/test_epoch)
 |  - Single batch (train_batch/test_batch)
 |  
 |  Method resolution order:
 |      Trainer
 |      abc.ABC
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, model: torch.nn.modules.module.Module, device: Union[torch.device, NoneType] = None)
 |      Initialize the trainer.
 |      :param model: Instance of the model to train.
 |      :param device: torch.device to run training on (CPU or GPU).
 |  
 |  fit(self, dl_train: torch.utils.data.dataloader.DataLoader, dl_test: torch.utils.data.dataloader.DataLoader, num_epochs: int, checkpoints: str = None, early_stopping: int = None, print_every: int = 1, **kw) -> cs236781.train_results.FitResult
 |      Trains the model for multiple epochs with a given training set,
 |      and calculates validation loss over a given validation set.
 |      :param dl_train: Dataloader for the training set.
 |      :param dl_test: Dataloader for the test set.
 |      :param num_epochs: Number of epochs to train for.
 |      :param checkpoints: Whether to save model to file every time the
 |          test set accuracy improves. Should be a string containing a
 |          filename without extension.
 |      :param early_stopping: Whether to stop training early if there is no
 |          test loss improvement for this number of epochs.
 |      :param print_every: Print progress every this number of epochs.
 |      :return: A FitResult object containing train and test losses per epoch.
 |  
 |  save_checkpoint(self, checkpoint_filename: str)
 |      Saves the model in it's current state to a file with the given name (treated
 |      as a relative path).
 |      :param checkpoint_filename: File name or relative path to save to.
 |  
 |  test_batch(self, batch) -> cs236781.train_results.BatchResult
 |      Runs a single batch forward through the model and calculates loss.
 |      :param batch: A single batch of data  from a data loader (might
 |          be a tuple of data and labels or anything else depending on
 |          the underlying dataset.
 |      :return: A BatchResult containing the value of the loss function and
 |          the number of correctly classified samples in the batch.
 |  
 |  test_epoch(self, dl_test: torch.utils.data.dataloader.DataLoader, **kw) -> cs236781.train_results.EpochResult
 |      Evaluate model once over a test set (single epoch).
 |      :param dl_test: DataLoader for the test set.
 |      :param kw: Keyword args supported by _foreach_batch.
 |      :return: An EpochResult for the epoch.
 |  
 |  train_batch(self, batch) -> cs236781.train_results.BatchResult
 |      Runs a single batch forward through the model, calculates loss,
 |      preforms back-propagation and updates weights.
 |      :param batch: A single batch of data  from a data loader (might
 |          be a tuple of data and labels or anything else depending on
 |          the underlying dataset.
 |      :return: A BatchResult containing the value of the loss function and
 |          the number of correctly classified samples in the batch.
 |  
 |  train_epoch(self, dl_train: torch.utils.data.dataloader.DataLoader, **kw) -> cs236781.train_results.EpochResult
 |      Train once over a training set (single epoch).
 |      :param dl_train: DataLoader for the training set.
 |      :param kw: Keyword args supported by _foreach_batch.
 |      :return: An EpochResult for the epoch.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __abstractmethods__ = frozenset({'test_batch', 'train_batch'})

The Trainer class splits the task of training (and evaluating) models into three conceptual levels,

  • Multiple epochs - the fit method, which returns a FitResult containing losses and accuracies for all epochs.
  • Single epoch - the train_epoch and test_epoch methods, which return an EpochResult containing losses per batch and the single accuracy result of the epoch.
  • Single batch - the train_batch and test_batch methods, which return a BatchResult containing a single loss and the number of correctly classified samples in the batch.

It implements the first two levels. Inheriting classes are expected to implement the single-batch level methods since these are model and/or task specific.

The first thing we should do in order to verify our model, gradient calculations and optimizer implementation is to try to overfit a large model (many parameters) to a small dataset (few images). This will show us that things are working properly.

Let's begin by loading the CIFAR-10 dataset.

In [6]:
data_dir = os.path.expanduser('~/.pytorch-datasets')
ds_train = torchvision.datasets.CIFAR10(root=data_dir, download=True, train=True, transform=tvtf.ToTensor())
ds_test = torchvision.datasets.CIFAR10(root=data_dir, download=True, train=False, transform=tvtf.ToTensor())

print(f'Train: {len(ds_train)} samples')
print(f'Test: {len(ds_test)} samples')
Files already downloaded and verified
Files already downloaded and verified
Train: 50000 samples
Test: 10000 samples

Now, let's implement just a small part of our training logic since that's what we need right now.

TODO:

  1. Complete the implementation of the train_batch() method in the LayerTrainer class within the hw2/training.py module.
  2. Update the hyperparameter values in the part2_overfit_hp() function in the hw2/answers.py module. Tweak the hyperparameter values until your model overfits a small number of samples in the code block below. You should get 100% accuracy within a few epochs.

The following code block will use your custom Layer-based MLP implentation, custom Vanilla SGD and custom trainer to overfit the data. The classification accuracy should be 100% within a few epochs.

In [7]:
import hw2.layers as layers
import hw2.answers as answers
from torch.utils.data import DataLoader

# Overfit to a very small dataset of 20 samples
batch_size = 10
max_batches = 2
dl_train = torch.utils.data.DataLoader(ds_train, batch_size, shuffle=False)

# Get hyperparameters
hp = answers.part2_overfit_hp()

torch.manual_seed(seed)

# Build a model and loss using our custom MLP and CE implementations
model = layers.MLP(3*32*32, num_classes=10, hidden_features=[128]*3, wstd=hp['wstd'])
loss_fn = layers.CrossEntropyLoss()

# Use our custom optimizer
optimizer = optimizers.VanillaSGD(model.params(), learn_rate=hp['lr'], reg=hp['reg'])

# Run training over small dataset multiple times
trainer = training.LayerTrainer(model, loss_fn, optimizer)
best_acc = 0
for i in range(20):
    res = trainer.train_epoch(dl_train, max_batches=max_batches)
    best_acc = res.accuracy if res.accuracy > best_acc else best_acc
    
test.assertGreaterEqual(best_acc, 98)

Now that we know training works, let's try to fit a model to a bit more data for a few epochs, to see how well we're doing. First, we need a function to plot the FitResults object.

In [8]:
from cs236781.plot import plot_fit
plot_fit?

TODO:

  1. Complete the implementation of the test_batch() method in the LayerTrainer class within the hw2/training.py module.
  2. Implement the fit() method of the Trainer class within the hw2/training.py module.
  3. Tweak the hyperparameters for this section in the part2_optim_hp() function in the hw2/answers.py module.
  4. Run the following code blocks to train. Try to get above 35-40% test-set accuracy.
In [9]:
# Define a larger part of the CIFAR-10 dataset (still not the whole thing)
batch_size = 50
max_batches = 100
in_features = 3*32*32
num_classes = 10
dl_train = torch.utils.data.DataLoader(ds_train, batch_size, shuffle=False)
dl_test = torch.utils.data.DataLoader(ds_test, batch_size//2, shuffle=False)
In [10]:
# Define a function to train a model with our Trainer and various optimizers
def train_with_optimizer(opt_name, opt_class, fig):
    torch.manual_seed(seed)
    
    # Get hyperparameters
    hp = answers.part2_optim_hp()
    hidden_features = [128] * 5
    num_epochs = 10
    
    # Create model, loss and optimizer instances
    model = layers.MLP(in_features, num_classes, hidden_features, wstd=hp['wstd'])
    loss_fn = layers.CrossEntropyLoss()
    optimizer = opt_class(model.params(), learn_rate=hp[f'lr_{opt_name}'], reg=hp['reg'])

    # Train with the Trainer
    trainer = training.LayerTrainer(model, loss_fn, optimizer)
    fit_res = trainer.fit(dl_train, dl_test, num_epochs, max_batches=max_batches)
    
    fig, axes = plot_fit(fit_res, fig=fig, legend=opt_name)
    return fig
In [11]:
fig_optim = None
fig_optim = train_with_optimizer('vanilla', optimizers.VanillaSGD, fig_optim)
--- EPOCH 1/10 ---
--- EPOCH 2/10 ---
--- EPOCH 3/10 ---
--- EPOCH 4/10 ---
--- EPOCH 5/10 ---
--- EPOCH 6/10 ---
--- EPOCH 7/10 ---
--- EPOCH 8/10 ---
--- EPOCH 9/10 ---
--- EPOCH 10/10 ---

Momentum¶

The simple vanilla SGD update is rarely used in practice since it's very slow to converge relative to other optimization algorithms.

One reason is that naïvely updating in the direction of the current gradient causes it to fluctuate wildly in areas where the loss surface in some dimensions is much steeper than in others. Another reason is that using the same learning rate for all parameters is not a great idea since not all parameters are created equal. For example, parameters associated with rare features should be updated with a larger step than ones associated with commonly-occurring features because they'll get less updates through the gradients.

Therefore more advanced optimizers take into account the previous gradients of a parameter and/or try to use a per-parameter specific learning rate instead of a common one.

Let's now implement a simple and common optimizer: SGD with Momentum. This optimizer takes previous gradients of a parameter into account when updating it's value instead of just the current one. In practice it usually provides faster convergence than the vanilla SGD.

The SGD with Momentum update rule can be stated as follows: $$\begin{align} \vec{v}_{t+1} &= \mu \vec{v}_t - \eta \delta \vec{\theta}_t \\ \vec{\theta}_{t+1} &= \vec{\theta}_t + \vec{v}_{t+1} \end{align}$$

Where $\eta$ is the learning rate, $\vec{\theta}$ is a model parameter, $\delta \vec{\theta}_t=\pderiv{L}{\vec{\theta}}(\vec{\theta}_t)$ is the gradient of the loss w.r.t. to the parameter and $0\leq\mu<1$ is a hyperparameter known as momentum.

Expanding the update rule recursively shows us now the parameter update infact depends on all previous gradient values for that parameter, where the old gradients are exponentially decayed by a factor of $\mu$ at each timestep.

Since we're incorporating previous gradient (update directions), a noisy value of the current gradient will have less effect so that the general direction of previous updates is maintained somewhat. The following figure illustrates this.

TODO:

  1. Complete the implementation of the MomentumSGD class in the hw2/optimizers.py module.
  2. Tweak the learning rate for momentum in part2_optim_hp() the function in the hw2/answers.py module.
  3. Run the following code block to compare to the vanilla SGD.
In [12]:
fig_optim = train_with_optimizer('momentum', optimizers.MomentumSGD, fig_optim)
fig_optim
--- EPOCH 1/10 ---
--- EPOCH 2/10 ---
--- EPOCH 3/10 ---
--- EPOCH 4/10 ---
--- EPOCH 5/10 ---
--- EPOCH 6/10 ---
--- EPOCH 7/10 ---
--- EPOCH 8/10 ---
--- EPOCH 9/10 ---
--- EPOCH 10/10 ---
Out[12]:

Bonus: RMSProp¶

This is another optmizer that accounts for previous gradients, but this time it uses them to adapt the learning rate per parameter.

RMSProp maintains a decaying moving average of previous squared gradients, $$ \vec{r}_{t+1} = \gamma\vec{r}_{t} + (1-\gamma)\delta\vec{\theta}_t^2 $$ where $0<\gamma<1$ is a decay constant usually set close to $1$, and $\delta\vec{\theta}_t^2$ denotes element-wise squaring.

The update rule for each parameter is then, $$ \vec{\theta}_{t+1} = \vec{\theta}_t - \left( \frac{\eta}{\sqrt{r_{t+1}+\varepsilon}} \right) \delta\vec{\theta}_t $$

where $\varepsilon$ is a small constant to prevent numerical instability. The idea here is to decrease the learning rate for parameters with high gradient values and vice-versa. The decaying moving average prevents accumulating all the past gradients which would cause the effective learning rate to become zero.

Bonus:

  1. Complete the implementation of the RMSProp class in the hw2/optimizers.py module.
  2. Tweak the learning rate for RMSProp in part2_optim_hp() the function in the hw2/answers.py module.
  3. Run the following code block to compare to the other optimizers.
In [13]:
fig_optim = train_with_optimizer('rmsprop', optimizers.RMSProp, fig_optim)
fig_optim
--- EPOCH 1/10 ---
--- EPOCH 2/10 ---
--- EPOCH 3/10 ---
--- EPOCH 4/10 ---
--- EPOCH 5/10 ---
--- EPOCH 6/10 ---
--- EPOCH 7/10 ---
--- EPOCH 8/10 ---
--- EPOCH 9/10 ---
--- EPOCH 10/10 ---
Out[13]:

Note that you should get better train/test accuracy with Momentum and RMSProp than Vanilla.

Dropout Regularization¶

Dropout is a useful technique to improve generalization of deep models.

The idea is simple: during the forward pass drop, i.e. set to to zero, the activation of each neuron, with a probability of $p$. For example, if $p=0.4$ this means we drop the activations of 40% of the neurons (on average).

There are a few important things to note about dropout:

  1. It is only performed during training. When testing our model the dropout layers should be a no-op.
  2. In the backward pass, gradients are only propagated back into neurons that weren't dropped during the forward pass.
  3. During testing, the activations must be scaled since the expected value of each neuron during the training phase is now $1-p$ times it's original expectation. Thus, we need to scale the test-time activations by $1-p$ to match. Equivalently, we can scale the train time activations by $1/(1-p)$.

TODO:

  1. Complete the implementation of the Dropout class in the hw2/layers.py module.
  2. Finish the implementation of the MLP's __init__() method in the hw2/layers.py module. If dropout>0 you should add a Dropout layer after each ReLU.
In [14]:
from hw2.grad_compare import compare_layer_to_torch

# Check architecture of MLP with dropout layers
mlp_dropout = layers.MLP(in_features, num_classes, [50]*3, dropout=0.6)
print(mlp_dropout)
test.assertEqual(len(mlp_dropout.sequence), 10)
for b1, b2 in zip(mlp_dropout.sequence, mlp_dropout.sequence[1:]):
    if str(b1).lower() == 'relu':
        test.assertTrue(str(b2).startswith('Dropout'))
test.assertTrue(str(mlp_dropout.sequence[-1]).startswith('Linear'))
MLP, Sequential
	[0] Linear(self.in_features=3072, self.out_features=50)
	[1] ReLU
	[2] Dropout(p=0.6)
	[3] Linear(self.in_features=50, self.out_features=50)
	[4] ReLU
	[5] Dropout(p=0.6)
	[6] Linear(self.in_features=50, self.out_features=50)
	[7] ReLU
	[8] Dropout(p=0.6)
	[9] Linear(self.in_features=50, self.out_features=10)

In [15]:
# Test end-to-end gradient in train and test modes.
print('Dropout, train mode')
mlp_dropout.train(True)
for diff in compare_layer_to_torch(mlp_dropout, torch.randn(500, in_features)):
    test.assertLess(diff, 1e-3)
    
print('Dropout, test mode')
mlp_dropout.train(False)
for diff in compare_layer_to_torch(mlp_dropout, torch.randn(500, in_features)):
    test.assertLess(diff, 1e-3)
Dropout, train mode
Comparing gradients... 
input    diff=0.000
param#01 diff=0.000
param#02 diff=0.000
param#03 diff=0.000
param#04 diff=0.000
param#05 diff=0.000
param#06 diff=0.000
param#07 diff=0.000
param#08 diff=0.000
Dropout, test mode
Comparing gradients... 
input    diff=0.000
param#01 diff=0.000
param#02 diff=0.000
param#03 diff=0.000
param#04 diff=0.000
param#05 diff=0.000
param#06 diff=0.000
param#07 diff=0.000
param#08 diff=0.000

To see whether dropout really improves generalization, let's take a small training set (small enough to overfit) and a large test set and check whether we get less overfitting and perhaps improved test-set accuracy when using dropout.

In [16]:
# Define a small set from CIFAR-10, but take a larger test set since we want to test generalization
batch_size = 10
max_batches = 40
in_features = 3*32*32
num_classes = 10
dl_train = torch.utils.data.DataLoader(ds_train, batch_size, shuffle=False)
dl_test = torch.utils.data.DataLoader(ds_test, batch_size*2, shuffle=False)

TODO: Tweak the hyperparameters for this section in the part2_dropout_hp() function in the hw2/answers.py module. Try to set them so that the first model (with dropout=0) overfits. You can disable the other dropout options until you tune the hyperparameters. We can then see the effect of dropout for generalization.

In [17]:
# Get hyperparameters
hp = answers.part2_dropout_hp()
hidden_features = [400] * 1
num_epochs = 30
In [18]:
torch.manual_seed(seed)
fig=None
#for dropout in [0]:  # Use this for tuning the hyperparms until you overfit
for dropout in [0, 0.4, 0.8]:
    model = layers.MLP(in_features, num_classes, hidden_features, wstd=hp['wstd'], dropout=dropout)
    loss_fn = layers.CrossEntropyLoss()
    optimizer = optimizers.MomentumSGD(model.params(), learn_rate=hp['lr'], reg=0)

    print('*** Training with dropout=', dropout)
    trainer = training.LayerTrainer(model, loss_fn, optimizer)
    fit_res_dropout = trainer.fit(dl_train, dl_test, num_epochs, max_batches=max_batches, print_every=6)
    fig, axes = plot_fit(fit_res_dropout, fig=fig, legend=f'dropout={dropout}', log_loss=True)
*** Training with dropout= 0
--- EPOCH 1/30 ---
--- EPOCH 7/30 ---
--- EPOCH 13/30 ---
--- EPOCH 19/30 ---
--- EPOCH 25/30 ---
--- EPOCH 30/30 ---
*** Training with dropout= 0.4
--- EPOCH 1/30 ---
--- EPOCH 7/30 ---
--- EPOCH 13/30 ---
--- EPOCH 19/30 ---
--- EPOCH 25/30 ---
--- EPOCH 30/30 ---
*** Training with dropout= 0.8
--- EPOCH 1/30 ---
--- EPOCH 7/30 ---
--- EPOCH 13/30 ---
--- EPOCH 19/30 ---
--- EPOCH 25/30 ---
--- EPOCH 30/30 ---

Questions¶

TODO Answer the following questions. Write your answers in the appropriate variables in the module hw2/answers.py.

In [19]:
from cs236781.answers import display_answer
import hw2.answers

Question 1¶

Regarding the graphs you got for the three dropout configurations:

  1. Explain the graphs of no-dropout vs dropout. Do they match what you expected to see?

    • If yes, explain why and provide examples based on the graphs.
    • If no, explain what you think the problem is and what should be modified to fix it.
  2. Compare the low-dropout setting to the high-dropout setting and explain based on your graphs.

In [20]:
display_answer(hw2.answers.part2_q1)

Your answer:

1.1: Our baseline is no dropout setting, with pre-tuned hyper-parameters for an overfitted model (overfitting can be inferred from the graphs, where the train accuracy is much higher than the test accuracy). We see that the training accuracy is higher without dropout in comparison to using dropout, but the test accuracy is lower for the same dropout settings comparison. This fits what we expected, as the dropout method "omits" training information and thus decrease the training accuracy, but due to its randomness, it makes the network less sensitive to the influence of single neurons and therefore demonstrate better generalization. Looking at our graphs, it is worth mention that as the dropout level increases, the training accuracy decreases (due to the reason we mentioned earlier). An opposite effect is seen in the test accuracy graphs, where the lower accuracy corresponds to higher dropout level (0.8) rather than the lower dropout level (0.4).

1.2: As we see in the graphs, the high-dropout setting had lower accuracy than the low-dropout setting, indicating a lower generalization competence for this setting. This might stem from the fact that too many neurons activations are being dropped on this setting (80% percent on average), leading to insufficient optimization in the training phase. As we expect, the best performance of the optimization is related to a medium level of dropout, which in our case is 0.4, in comparison to 0 (no dropout) and to 0.8 (high-dropout).

Question 2¶

When training a model with the cross-entropy loss function, is it possible for the test loss to increase for a few epochs while the test accuracy also increases?

If it's possible explain how, if it's not explain why not.

In [21]:
display_answer(hw2.answers.part2_q2)

Your answer:

When training a model with the cross-entropy loss function, it is possible for the test loss to increase for a few epochs while the test accuracy also increases.

We can look at an example for a single epoch:

  • We have 3 classes and 10 examples.
  • Our model gave probability $\frac{1}{3} + \epsilon$ for the correct class, and a little less for each other class.
  • Thus, our model's accuracy is very high (in this example it's 100%).
  • And on the other hand, our model's loss is high because the CE loss is proportional to the probability given to the correct class.
$$ L_{CE}(\hat{y},y) = -\sum_{k=1}^K\mathbb{I}\left[ y=k \right]\log{\frac{e^{p_k}}{\sum^K_{j=1}e^{p_j}}} \propto p_y $$

To conclude, if this happens for each epoch with decreasing $\epsilon$, then the behavior will be as described.

Question 3¶

  1. Explain the difference between gradient descent and back-propagation.

  2. Compare in detail between gradient descent (GD) and stochastic gradient descent (SGD).

  3. Why is SGD used more often in the practice of deep learning? Provide a few justifications.

  4. You would like to try GD to train your model instead of SGD, but you're concerned that your dataset won't fit in memory. A friend suggested that you should split the data into disjoint batches, do multiple forward passes until all data is exhausted, and then do one backward pass on the sum of the losses.

    1. Would this approach produce a gradient equivalent to GD? Why or why not? provide mathematical justification for your answer.
    2. You implemented the suggested approach, and were careful to use batch sizes small enough so that each batch fits in memory. However, after some number of batches you got an out of memory error. What happened?
In [22]:
display_answer(hw2.answers.part2_q3)

Your answer:

Answer 1¶

In essence, gradient descent is the optimization process, while backpropagation is the algorithm used within that process to compute the gradients.

Gradient descent:

  • Gradient descent is an optimization algorithm used to minimize a loss function.
  • It iteratively adjusts the model parameters by moving in the direction of the steepest descent of the loss function to find the minimum.
  • It can use the backpropagation algorithm to compute the gradients of the model parameters more efficiently.

Backpropagation:

  • Backpropagation is an algorithm used to compute the gradients of the model parameters, usually in a neural network.
  • It propagates the error gradients backward through the network, allowing for efficient calculation of gradients needed for gradient descent.
  • For large neural networks, the gradient calculations can be infeasible without the backpropagation algorithm.

Answer 2¶

  1. Batch updates: In GD, the entire training dataset is used to compute the gradient of the cost function before updating the model parameters.

In SGD, only a single training example (or a mini-batch) is randomly selected and used to compute the gradient and update the parameters.

  1. Efficiency: As GD considers the entire dataset for each parameter update, it can be computationally expensive, especially for large datasets.

SGD is more efficient compared to GD as it uses a single example for the parameter update.

  1. Convergence: GD converges to the global minimum if the loss function is convex, the convergence is generally slower as it takes more steps to reach the minimum.

SGD might not converge to the global minimum due to the randomness introduced by selecting the examples. While it might not reach the optimal solution, it often finds a good enough solution in practice and does that faster than GD.

  1. Noise and Regularization: GD uses the entire dataset, resulting a smoother parameter updates.

SGD introduces randomness to the parameter updates, making it more noisy, but allowing it to escape local minima sometimes, making it an implicit form of regularization.

Answer 3¶

SGD is commonly used in the practice of deep learning for several reasons:

  1. Efficiency with large datasets: Deep learning often involves working with massive datasets that may not fit entirely into memory.

SGD's ability to update parameters based on individual examples (or mini-batches) makes it more efficient.

  1. Faster convergence: While SGD's updates tends to be noisier than GD's, it can actually lead to faster convergence in practice.

  2. Generalization: SGD's inherent stochastically during training acts as a form of regularization, preventing overfitting and promoting better generalization.

  3. Avoiding local minima: Deep learning models often have complex, high-dimensional loss landscapes with numerous local minima. GD relies on the entire dataset and may get trapped in suboptimal local minima.

SGD's stochastic nature provides a mechanism for escaping these suboptimal solutions.

Answer 4¶

  1. Yes, this method will produce a gradient equivalent to GD. We remember that:
$$ L(\vec{\theta}) = \frac{1}{N} \sum_{i=1}^{N} \ell(\vec{y}^i, \hat{\vec{y}^i}) + R(\vec{\theta}) $$

We can re-write the above using $K$ disjoint batches (assume we keep the same order for the $i$ 's): $$ L(\vec{\theta}) = \frac{1}{N} \sum_{j=1}^{K} \sum_{i \in B_j} \ell(\vec{y}^i, \hat{\vec{y}^i}) + R(\vec{\theta}) $$ Thus, the forward calculation is equivalent.

Now, we have $L(\vec{\theta})$ and wish to calculate $\frac{\partial L}{\partial \vec{x}}$, we don't need the dataset anymore for this calculation, so it is equivalent to GD's.

  1. The most probable thing that happened is that during each forward pass, we accumulate the activation's values and store them in the memory.

Thus, after multiple forward passes, the memory could be filled with the values we accumulated.

Question 4 (Automatic Differentiation)¶

Let $f = f_n \circ f_{n-1} \circ ... \circ f_1$ where each $f_i: \mathbb{R} \rightarrow \mathbb{R}$ is a differentiable function which is easy to evaluate and differentiate (each query costs $\mathcal{O}(1)$ at a given point).

  1. In this exercise you will reduce the memory complexity for evaluating $\nabla f (x_0)$ at some point $x_0$.

Assume that you are given with $f$ already expressed as a computational graph and a point $x_0$. 1. Show how to reduce the memory complexity for computing the gradient using forward mode AD (maintaining the $\mathcal{O}(n)$ computation cost). What is the memory complexity? 2. Show how to reduce the memory complexity for computing the gradient using backward mode AD (maintaining the $\mathcal{O}(n)$ computation cost). What is the memory complexity? 2. Can these techniques be generalized for arbitrary computational graphs? 3. Think how the backprop algorithm can benefit from these techniques when applied to deep architectures (e.g VGGs, ResNets).

In [23]:
display_answer(hw2.answers.part2_q4)

Your answer:

  1. Using the chain rule we get:
$$ \frac{\partial f}{\partial x_0} = \frac{\partial f}{\partial f_n} \frac{\partial f_n}{\partial f_{n-1}} \cdots \frac{\partial f_1}{\partial x_0} $$

In forward mode:

We can calculate the derivative with the following equations: $$ \begin{align} \frac{\partial f_1}{\partial x_0} &\\ \frac{\partial f_2}{\partial x_0} &= \frac{\partial f_2}{\partial f_1} \frac{\partial f_1}{\partial x_0} \\ & \vdots \\ \frac{\partial f_n}{\partial x_0} &= \frac{\partial f_{n-1}}{\partial f_{n-2}} \frac{\partial f_{n-2}}{\partial x_0} \\ \frac{\partial f}{\partial x_0} &= \frac{\partial f}{\partial f_n} \frac{\partial f_n}{\partial x_0} \\ \end{align} $$

We can compute the above using only 2 variables to store the calculations - accumulated derivative $\delta$ and current derivative $d$: $$ \begin{align*} &1.~ \text{Init:} \\ & \quad d_1 \leftarrow \frac{\partial f_1}{\partial x_0} \\ & \quad \delta_1 \leftarrow d \\ &2.~ \text{for}~ i=2,...,n:\\ & \quad d_i \leftarrow \frac{\partial f_i}{\partial f_{i-1}} \\ & \quad \delta_i \leftarrow \delta_{i-1} \cdot d_i \\ &3.~ d_f \leftarrow \frac{\partial f}{\partial f_n} \\ &4.~ \text{Return}~ \delta_n \cdot d_f \\ \end{align*} $$

This way, we reduced the memory complexity to $\mathcal{O}(1)$ while keeping time complexity at $\mathcal{O}(n)$.

In backward mode:

This time, we can calculate the derivative with the following equations: $$ \begin{align} \frac{\partial f}{\partial f_n} &\\ \frac{\partial f}{\partial f_{n-1}} &= \frac{\partial f}{\partial f_n} \frac{\partial f_n}{\partial f_{n-1}} \\ & \vdots \\ \frac{\partial f}{\partial f_1} &= \frac{\partial f}{\partial f_2} \frac{\partial f_2}{\partial f_1} \\ \frac{\partial f}{\partial x_0} &= \frac{\partial f}{\partial f_1} \frac{\partial f_1}{\partial x_0} \\ \end{align} $$ We can compute the above using the following algorithm; $$ \begin{align*} &1.~ \text{Init:} \\ & \quad d_n \leftarrow \frac{\partial f}{\partial f_n} \\ & \quad \delta_1 \leftarrow d \\ &2.~ \text{for}~ i=n,...,2:\\ & \quad d_i \leftarrow \frac{\partial f_i}{\partial f_{i-1}} \\ & \quad \delta_i \leftarrow \delta_{i+1} \cdot d_i \\ &3.~ d_f \leftarrow \frac{\partial f_1}{\partial x_0} \\ &4.~ \text{Return}~ \delta_n \cdot d_f \\ \end{align*} $$

Again, we reduced the memory complexity to $\mathcal{O}(1)$ while keeping time complexity at $\mathcal{O}(n)$.

  1. Yes, this technique can be generalized for any arbitrary computational graphs.

The change that we need to make is computing the gradient for each input (in forward mode) or for each output (in backward mode). This will increase the memory usage to $\mathcal{O} \left( \text{\# \{ inputs or outputs \}} \right)$.

  1. In deep network architectures, it is common that the number of outputs is smaller than the number of inputs

(i.e., the input is a 32x32x3 image - 3072 inputs, while the output is a vector of size $C$ - the number of classes, which is much smaller).
Thus, the the backprop algorithm with the backward mode will benefit from these techniques because the memory cost will be much lower.

$$ \newcommand{\mat}[1]{\boldsymbol {#1}} \newcommand{\mattr}[1]{\boldsymbol {#1}^\top} \newcommand{\matinv}[1]{\boldsymbol {#1}^{-1}} \renewcommand{\vec}[1]{\boldsymbol {#1}} \newcommand{\vectr}[1]{\boldsymbol {#1}^\top} \newcommand{\rvar}[1]{\mathrm {#1}} \newcommand{\rvec}[1]{\boldsymbol{\mathrm{#1}}} \newcommand{\diag}{\mathop{\mathrm {diag}}} \renewcommand{\set}[1]{\mathbb {#1}} \newcommand{\norm}[1]{\left\lVert#1\right\rVert} \newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}} \newcommand{\bb}[1]{\boldsymbol{#1}} $$

Part 3: Binary Classification with Multilayer Perceptrons¶

In this part we'll implement a general purpose MLP and Binary Classifier using pytorch. We'll implement its training, and also learn about decision boundaries an threshold selection in the context of binary classification. Finally, we'll explore the effect of depth and width on an MLP's performance.

In [1]:
import os
import re
import sys
import glob
import unittest
from typing import Sequence, Tuple

import sklearn
import numpy as np
import matplotlib.pyplot as plt
import torch
import torchvision
import torch.nn as nn
import torchvision.transforms as tvtf
from torch import Tensor

%matplotlib inline
%load_ext autoreload
%autoreload 2
In [2]:
seed = 42
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
plt.rcParams.update({'font.size': 12})
test = unittest.TestCase()

Synthetic Dataset¶

To test our first neural network-based classifiers we'll start by creating a toy binary classification dataset, but one which is not trivial for a linear model.

In [3]:
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
In [4]:
def rotate_2d(X, deg=0):
    """
    Rotates each 2d sample in X of shape (N, 2) by deg degrees.
    """
    a = np.deg2rad(deg)
    return X @ np.array([[np.cos(a), -np.sin(a)],[np.sin(a), np.cos(a)]]).T

def plot_dataset_2d(X, y, n_classes=2, alpha=0.2, figsize=(8, 6), title=None, ax=None):
    if ax is None:
        fig, ax = plt.subplots(1, 1, figsize=figsize)
    for c in range(n_classes):
        ax.scatter(*X[y==c,:].T, alpha=alpha, label=f"class {c}");
        
    ax.set_xlabel("$x_1$"); ax.set_ylabel("$x_2$");
    ax.legend(); ax.set_title((title or '') + f" (n={len(y)})")

We'll split our data into 80% train and validation, and 20% test. To make it a bit more challenging, we'll simulate a somewhat real-world setting where there are multiple populations, and the training/validation data is not sampled iid from the underlying data distribution.

In [5]:
np.random.seed(seed)

N = 10_000
N_train = int(N * .8)

# Create data from two different distributions for the training/validation
X1, y1 = make_moons(n_samples=N_train//2, noise=0.2)
X1 = rotate_2d(X1, deg=10)
X2, y2 = make_moons(n_samples=N_train//2, noise=0.25)
X2 = rotate_2d(X2, deg=50)

# Test data comes from a similar but noisier distribution
X3, y3 = make_moons(n_samples=(N-N_train), noise=0.3)
X3 = rotate_2d(X3, deg=40)

X, y = np.vstack([X1, X2, X3]), np.hstack([y1, y2, y3])
In [6]:
# Train and validation data is from mixture distribution
X_train, X_valid, y_train, y_valid = train_test_split(X[:N_train, :], y[:N_train], test_size=1/3, shuffle=False)

# Test data is only from the second distribution
X_test, y_test = X[N_train:, :], y[N_train:]

fig, ax = plt.subplots(1, 3, figsize=(20, 5))
plot_dataset_2d(X_train, y_train, title='Train', ax=ax[0]);
plot_dataset_2d(X_valid, y_valid, title='Validation', ax=ax[1]);
plot_dataset_2d(X_test, y_test, title='Test', ax=ax[2]);

Now let us create a data loader for each dataset.

In [7]:
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader

batch_size = 32

dl_train, dl_valid, dl_test = [
    DataLoader(
        dataset=TensorDataset(
            torch.from_numpy(X_).to(torch.float32),
            torch.from_numpy(y_)
        ),
        shuffle=True,
        num_workers=0,
        batch_size=batch_size
    )
    for X_, y_ in [(X_train, y_train), (X_valid, y_valid), (X_test, y_test)]
]

print(f'{len(dl_train.dataset)=}, {len(dl_valid.dataset)=}, {len(dl_test.dataset)=}')
len(dl_train.dataset)=5333, len(dl_valid.dataset)=2667, len(dl_test.dataset)=2000

Simple MLP¶

A multilayer-perceptron is arguably a the most basic type of neural network model. It is composed of $L$ layers, each layer $l$ with $n_l$ perceptron ("neuron") units. Each perceptron is connected to all ouputs of the previous layer (or all inputs in the first layer), calculates their weighted sum, applies a linearity and produces a single output.

Each layer $l$ operates on the output of the previous layer ($\vec{y}_{l-1}$) and calculates:

$$ \vec{y}_l = \varphi\left( \mat{W}_l \vec{y}_{l-1} + \vec{b}_l \right),~ \mat{W}_l\in\set{R}^{n_{l}\times n_{l-1}},~ \vec{b}_l\in\set{R}^{n_l},~ l \in \{1,2,\dots,L\}. $$
  • Note that both input and output are vectors. We can think of the above equation as describing a layer of multiple perceptrons.
  • We'll henceforth refer to such layers as fully-connected or FC layers.
  • The first layer accepts the input of the model, i.e. $\vec{y}_0=\vec{x}\in\set{R}^d$.
  • The last layer, $L$, is the output layer, so $y_L$ is the output of the model.
  • The layers $1, 2, \dots, L-1$ are called hidden layers.

To begin, let's implement a general multi-layer perceptron model. We'll seek to implement it in a way which is both general in terms of architecture, and also composable so that we can use our MLP in the context of larger models.

TODO: Implement the MLP class in the hw2/mlp.py module.

In [8]:
from hw2.mlp import MLP

mlp = MLP(
    in_dim=2,
    dims=[8, 16, 32, 64],
    nonlins=['relu', 'tanh', nn.LeakyReLU(0.314), 'softmax']
)
mlp
Out[8]:
MLP(
  (sequence): Sequential(
    (0): Linear(in_features=2, out_features=8, bias=True)
    (1): ReLU()
    (2): Linear(in_features=8, out_features=16, bias=True)
    (3): Tanh()
    (4): Linear(in_features=16, out_features=32, bias=True)
    (5): LeakyReLU(negative_slope=0.314)
    (6): Linear(in_features=32, out_features=64, bias=True)
    (7): Softmax(dim=1)
  )
)

Let's try our implementation on a batch of data.

In [9]:
x0, y0 = next(iter(dl_train))

yhat0 = mlp(x0)

test.assertEqual(len([*mlp.parameters()]), 8)
test.assertEqual(yhat0.shape, (batch_size, mlp.out_dim))
test.assertTrue(torch.allclose(torch.sum(yhat0, dim=1), torch.tensor(1.0)))
test.assertIsNotNone(yhat0.grad_fn)

yhat0
Out[9]:
tensor([[0.0194, 0.0164, 0.0185,  ..., 0.0140, 0.0151, 0.0156],
        [0.0190, 0.0168, 0.0195,  ..., 0.0137, 0.0152, 0.0157],
        [0.0185, 0.0167, 0.0201,  ..., 0.0134, 0.0156, 0.0159],
        ...,
        [0.0193, 0.0163, 0.0173,  ..., 0.0146, 0.0154, 0.0152],
        [0.0184, 0.0166, 0.0186,  ..., 0.0139, 0.0157, 0.0157],
        [0.0192, 0.0165, 0.0166,  ..., 0.0150, 0.0149, 0.0151]],
       grad_fn=<SoftmaxBackward0>)

MLP for Binary Classification¶

The MLP model we've implemented, while useful, is very general. For the task of binary classification, we would like to add some additional functionality to it: the ability to output a normalized score for a sample being in class one (which we interpret as a probability) and a prediction based on some threshold of this probability. In addition, we need some way to calculate a meaningful threshold based on the data and a trained model at hand.

In order to maintain generality, we'll add this functionlity in the form of a wrapper: A BinaryClassifier class that can wrap any model producing two output features, and provide the the functionality stated above.

TODO: In the hw2/classifier.py module, implement the BinaryClassifier and the missing parts of its base class, Classifier. Read the method documentation carefully and implement accordingly. You can ignore the roc_threshold method at this stage.

In [10]:
from hw2.classifier import BinaryClassifier

bmlp4 = BinaryClassifier(
    model=MLP(in_dim=2, dims=[*[10]*3, 2], nonlins=[*['relu']*3, 'none']),
    threshold=0.5
)
print(bmlp4)

# Test model
test.assertEqual(len([*bmlp4.parameters()]), 8)
test.assertIsNotNone(bmlp4(x0).grad_fn)

# Test forward
yhat0_scores = bmlp4(x0)
test.assertEqual(yhat0_scores.shape, (batch_size, 2))
test.assertFalse(torch.allclose(torch.sum(yhat0_scores, dim=1), torch.tensor(1.0)))

# Test predict_proba
yhat0_proba = bmlp4.predict_proba(x0)
test.assertEqual(yhat0_proba.shape, (batch_size, 2))
test.assertTrue(torch.allclose(torch.sum(yhat0_proba, dim=1), torch.tensor(1.0)))

# Test classify
yhat0 = bmlp4.classify(x0)
test.assertEqual(yhat0.shape, (batch_size,))
test.assertEqual(yhat0.dtype, torch.int)
test.assertTrue(all(yh_ in (0, 1) for yh_ in yhat0))
BinaryClassifier(
  (model): MLP(
    (sequence): Sequential(
      (0): Linear(in_features=2, out_features=10, bias=True)
      (1): ReLU()
      (2): Linear(in_features=10, out_features=10, bias=True)
      (3): ReLU()
      (4): Linear(in_features=10, out_features=10, bias=True)
      (5): ReLU()
      (6): Linear(in_features=10, out_features=2, bias=True)
      (7): Identity()
    )
  )
)

Training¶

Now that we have a classifier, we need to train it. We will abstract the various aspects of training such as mlutiple epochs, iterating over batches, early stopping and saving model checkpoints, into a Trainer that will take care of these concerns.

The Trainer class splits the task of training (and evaluating) models into three conceptual levels,

  • Multiple epochs - the fit method, which returns a FitResult containing losses and accuracies for all epochs.
  • Single epoch - the train_epoch and test_epoch methods, which return an EpochResult containing losses per batch and the single accuracy result of the epoch.
  • Single batch - the train_batch and test_batch methods, which return a BatchResult containing a single loss and the number of correctly classified samples in the batch.

It implements the first two levels. Inheriting classes are expected to implement the single-batch level methods since these are model and/or task specific.

TODO:

  1. Implement the Trainer's fit method and the ClassifierTrainer's train_batch/test_batch methods, in the hw2/training.py module. You may ignore the Optional parts about early stopping an model checkpoints at this stage.

  2. Set the model's architecture hyper-parameters and the optimizer hyperparameters in part3_arch_hp() and part3_optim_hp(), respectively, in hw2/answers.py.

Since this is a toy dataset, you should be able to quickly get above 85% accuracy even on the test set.

In [11]:
from hw2.training import ClassifierTrainer
from hw2.answers import part3_arch_hp, part3_optim_hp

torch.manual_seed(seed)

hp_arch = part3_arch_hp()
hp_optim = part3_optim_hp()

model = BinaryClassifier(
    model=MLP(
        in_dim=2,
        dims=[*[hp_arch['hidden_dims'],]*hp_arch['n_layers'], 2],
        nonlins=[*[hp_arch['activation'],]*hp_arch['n_layers'], hp_arch['out_activation']]
    ),
    threshold=0.5,
)
print(model)

loss_fn = hp_optim.pop('loss_fn')
optimizer = torch.optim.SGD(params=model.parameters(), **hp_optim)
trainer = ClassifierTrainer(model, loss_fn, optimizer)

fit_result = trainer.fit(dl_train, dl_valid, num_epochs=20, print_every=10);

test.assertGreaterEqual(fit_result.train_acc[-1], 85.0)
test.assertGreaterEqual(fit_result.test_acc[-1], 75.0)
BinaryClassifier(
  (model): MLP(
    (sequence): Sequential(
      (0): Linear(in_features=2, out_features=10, bias=True)
      (1): Tanh()
      (2): Linear(in_features=10, out_features=10, bias=True)
      (3): Tanh()
      (4): Linear(in_features=10, out_features=10, bias=True)
      (5): Tanh()
      (6): Linear(in_features=10, out_features=2, bias=True)
      (7): Identity()
    )
  )
)
--- EPOCH 1/20 ---
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
--- EPOCH 11/20 ---
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
--- EPOCH 20/20 ---
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
In [12]:
from cs236781.plot import plot_fit

plot_fit(fit_result, log_loss=False, train_test_overlay=True);

Decision Boundary¶

An important part of understanding what a non-linear classifier like our MLP is doing is visualizing it's decision boundaries. When we only have two input features, these are relatively simple to visualize, since we can simply plot our data on the plane, and evaluate our classifier on a constant 2D grid in order to approximate the decision boundary.

TODO: Implement the plot_decision_boundary_2d function in the hw2/classifier.py module.

In [13]:
from hw2.classifier import plot_decision_boundary_2d

fig, ax = plot_decision_boundary_2d(model, *dl_valid.dataset.tensors)
/home/ofer.nissim/miniconda3/envs/cs236781-hw/lib/python3.8/site-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  /opt/conda/conda-bld/pytorch_1639180588308/work/aten/src/ATen/native/TensorShape.cpp:2157.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]

Threshold Selection¶

Another important component, especially in the context of binary classification is threshold selection. Until now, we arbitrarily chose a threshold of 0.5 when deciding the class label based on the probability score we calculated via softmax. In other words, we classified a sample to class 1 (the 'positive' class) when it's probability score was greater or equal to 0.5.

However, in real-world classifiction problems we'll need to choose our threshold wisely based on the domain-specific requirements of the problem. For example, depending on our application, we might care more about high sensitivity (correctly classifying positive examples), while for other applications specificity (correctly classifying negative examples) is more important.

One way to understand the mistakes a model is making is to look at its Confusion Matrix. From it, we easily see e.g. the false-negative rate (FNR) and false-positive rate (FPR).

Let's look at the confusion matrices on the test and validation data using the model we trained above.

In [14]:
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

def plot_confusion(classifier, x: np.ndarray, y: np.ndarray, ax=None):
    y_hat = classifier.classify(torch.from_numpy(x).to(torch.float32)).numpy()
    conf_mat = confusion_matrix(y, y_hat, normalize='all')
    ConfusionMatrixDisplay(conf_mat).plot(ax=ax, colorbar=False)
    
model.threshold = 0.5

_, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].set_title("Train"); axes[1].set_title("Validation");
plot_confusion(model, X_train, y_train, ax=axes[0])
plot_confusion(model, X_valid, y_valid, ax=axes[1])

We can see that the model makes a different number of false-posiive and false-negative errors. Clearly, this proportion would change if the classification threshold was different.

A very common way to select the classification threshold is to find a threshold which optimally balances between the FPR and FNR. This can be done by plotting the model's ROC curve, which shows 1-FNR vs. FPR for multiple threshold values, and selecting the point closest to the ideal point ((0, 1)).

TODO: Implement the select_roc_thresh function in the hw2.classifier module.

In [15]:
from hw2.classifier import select_roc_thresh


optimal_thresh = select_roc_thresh(model, *dl_valid.dataset.tensors, plot=True)

Let's see the effect of our threshold selection on the confusion matrix and decision boundary.

In [16]:
model.threshold = optimal_thresh

_, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].set_title("Train"); axes[1].set_title("Validation");
plot_confusion(model, X_train, y_train, ax=axes[0])
plot_confusion(model, X_valid, y_valid, ax=axes[1])
fig, ax = plot_decision_boundary_2d(model, *dl_valid.dataset.tensors)

Architecture Experiments¶

Now, equipped with the tools we've implemented so far we'll expertiment with various MLP architectures. We'll seek to study the effect of the models depth (number of hidden layers) and width (number of neurons per hidden layer) on the its decision boundaries and the resulting performance. After training, we will use the validation set for threshold selection, and seek to maximize the performance on the test set.

TODO: Implement the mlp_experiment function in hw2/experiments.py. You are free to configure any model and optimization hyperparameters however you like, except for the specified width and depth. Experiment with various options for these other hyperparameters and try to obtain the best results you can.

In [17]:
from itertools import product
from tqdm.auto import tqdm
from hw2.experiments import mlp_experiment

torch.manual_seed(seed)

depths = [1, 2, 4]
widths = [2, 8, 32]
exp_configs = product(enumerate(widths), enumerate(depths))
fig, axes = plt.subplots(len(widths), len(depths), figsize=(10*len(depths), 10*len(widths)), squeeze=False)
test_accs = []

for (i, width), (j, depth) in tqdm(list(exp_configs)):
    model, thresh, valid_acc, test_acc = mlp_experiment(
        depth, width, dl_train, dl_valid, dl_test, n_epochs=10
    )
    test_accs.append(test_acc)
    fig, ax = plot_decision_boundary_2d(model, *dl_test.dataset.tensors, ax=axes[i, j])
    ax.set_title(f"{depth=}, {width=}")
    ax.text(ax.get_xlim()[0]*.95, ax.get_ylim()[1]*.95, f"{thresh=:.2f}\n{valid_acc=:.1f}%\n{test_acc=:.1f}%", va="top")
    
# Assert minimal performance requirements.
# You should be able to do better than these by at least 5%.
test.assertGreaterEqual(np.min(test_accs), 75.0)
test.assertGreaterEqual(np.quantile(test_accs, 0.75), 85.0)
  0%|          | 0/9 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]
train_batch:   0%|          | 0/167 [00:00<?, ?it/s]
test_batch:   0%|          | 0/84 [00:00<?, ?it/s]

Questions¶

TODO Answer the following questions. Write your answers in the appropriate variables in the module hw2/answers.py.

In [18]:
from cs236781.answers import display_answer
import hw2.answers

Question 1¶

Consider the first binary classifier you trained in this notebook and the loss/accuracy curves we plotted for it on the train and validation sets, as well as the decision boundary plot.

Based on those plots, explain qualitatively whether or now your model has:

  1. High Optimization error?
  2. High Generalization error?
  3. High Approximation error?

Explain your answers for each of the above. Since this is a qualitative question, assume "high" simply means "I would take measures in order to decrease it further".

In [19]:
display_answer(hw2.answers.part3_q1)

Your answer:

  1. Optimization error - low

This error is between the best predictor we can get from the training set, $\theta^*_{\text{training}}$ and the best predictor we can get by the optimization $\theta^*_{\text{optimization}}$.
The signs for high optimization error are: slow convergence, instability, or erratic behavior during training.
As we can see in the graph, the training proccess gives us a nice, smooth convergence to a high accuracy, so this error is probably low.

  1. Generalization error - high

This error is between the ideal predictor for the architecture we chose, $\theta^*_{\text{world}}$ the best predictor we can get from the training set, $\theta^*_{\text{training}}$.
The signs for high generalization error are: overfitting (high training accuracy but low validation/test accuracy, and poor performance on new data).
As we can see in the graph, we get high training accuracy but our validation accuracy is erratic and low. So we can conclude that our generalization error is high.

  1. Approximation error - low

This error is between the ideal predictor (overall) $\theta^*$ and the ideal predictor for the architecture we chose, $\theta^*_{\text{world}}$
The signs for high approximation error are: underfitting (high training and validation errors, low accuracy, and poor predictive performance).
As we can see in the graph, we get high training accuracy (and low error) so our approximation error is probably low.

Question 2¶

Consider the first binary classifier you trained in this notebook and the confusion matrices we plotted for it.

For the validation dataset, would you expect the FPR or the FNR to be higher, and why? Recall that you have full knowledge of the data generating process.

In [20]:
display_answer(hw2.answers.part3_q2)

Your answer:

In the data generation process, we know that the validation data has more noise than the training data (0.25 in comparison to 0.2). Both datasets were rotated in a different angle, a manipulation that adds more noise to the data. Nevertheless, due to the symmetry of the "moons" shapes - it does not change the proportion of FPR to FNR. therefore, understand the both FPR and FNR become higher in the validation set, with relatively same values, according to the symmetry we mentioned above.

Question 3¶

You're training a binary classifier screening of a large cohort of patients for some disease, with the aim to detect the disease early, before any symptoms appear. You train the model on easy-to-obtain features, so screening each individual patient is simple and low-cost. In case the model classifies a patient as sick, she must then be sent to furhter testing in order to confirm the illness. Assume that these further tests are expensive and involve high-risk to the patient. Assume also that once diagnosed, a low-cost treatment exists.

You wish to screen as many people as possible at the lowest possible cost and loss of life. Would you still choose the same "optimal" point on the ROC curve as above? If not, how would you choose it? Answer these questions for two possible scenarios:

  1. A person with the disease will develop non-lethal symptoms that immediately confirm the diagnosis and can then be treated.
  2. A person with the disease shows no clear symptoms and may die with high probability if not diagnosed early enough, either by your model or by the expensive test.

Explain your answers.

In [21]:
display_answer(hw2.answers.part3_q3)

Your answer:

You wish to screen as many people as possible at the lowest possible cost and loss of life. Would you still choose the same "optimal" point on the ROC curve as above? If not, how would you choose it? Answer these questions for two possible scenarios:

  1. A person with the disease will develop non-lethal symptoms that immediately confirm the diagnosis and can then be treated.
  2. A person with the disease shows no clear symptoms and may die with high probability if not diagnosed early enough, either by your model or by the expensive test.

Scenario 1 - non-lethal symptoms

  • Because the symptoms are non-lethal, and confirm the diagnosis.
  • So, we would choose a threshold with more weight towards lower FPR.
  • This means that less patients will falsely get diagnosed positive for the illness and get the expensive tests to confirm the diagnosis.
  • By that, we reduced the cost while maintaining the low loss of life.

Scenario 2 - lethal illness and no clear symptoms

  • The illness is lethal and the symptoms are not clear, so we must allow more false-negatives to be able to save lives.
  • Thus, we should choose the threshold with more weight towards the low FNR.
  • This means that more patients will be falsely diagnosed for the illness, and start the treatment to save their lives

and that the expensive test will give us the true results later on.

  • By that, we reduced the loss of life while increasing the cost by a little (more expensive tests).

Question 4¶

Analyze your results from the Architecture Experiment.

  1. Explain the decision boundaries and model performance you obtained for the columns (fixed depth, width varies).
  2. Explain the decision boundaries and model performance you obtained for the rows (fixed width, depth varies).
  3. Compare and explain the results for the following pair of configurations, which have the same number of total parameters:
    • depth=1, width=32 and depth=4, width=8
  4. Explain the effect of threshold selection on the validation set: did it improve the results on the test set? why?
In [22]:
display_answer(hw2.answers.part3_q4)

Your answer:

Analyze your results from the Architecture Experiment.

  1. For fixed depth and varying width, we can see that the performance increases as we increase the width, but at some point the high width tends to create overfit,

we can see that by the drop in the accuracies at the bottom model of each column. 2. For fixed width and varying height, we can see the same behavior as in the first answer above. Increasing the depth makes the model more expressive, but at some point it creates overfit. 3. The two models, although having the same number of parameters, have huge difference in performance. The depth=1, width=32 has a very poor performance, the low depth enforces a low expressivity, making the model not much different from a regulat SVM model (linear decision boundary).
On the other hand, the depth=4, width=8 is the best performing model in terms of validation and test accuracy. The depth and width are in a good balance and making the model both very expressive, and very generalized for the data. 4. The threshold selection method was performed the validation set and not on the train set, meaning that the model was able to find its optimal hyperparameter and optimize its performance according to new samples (no data leakage). This aided to provide a better generalization and a better accuracy on the test set.

$$ \newcommand{\mat}[1]{\boldsymbol {#1}} \newcommand{\mattr}[1]{\boldsymbol {#1}^\top} \newcommand{\matinv}[1]{\boldsymbol {#1}^{-1}} \renewcommand{\vec}[1]{\boldsymbol {#1}} \newcommand{\vectr}[1]{\boldsymbol {#1}^\top} \newcommand{\rvar}[1]{\mathrm {#1}} \newcommand{\rvec}[1]{\boldsymbol{\mathrm{#1}}} \newcommand{\diag}{\mathop{\mathrm {diag}}} \renewcommand{\set}[1]{\mathbb {#1}} \newcommand{\norm}[1]{\left\lVert#1\right\rVert} \newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}} \newcommand{\bb}[1]{\boldsymbol{#1}} $$

Part 4: Convolutional Neural Networks¶

In this part we will explore convolution networks. We'll implement a common block-based deep CNN pattern with an without residual connections.

In [1]:
import os
import re
import sys
import glob
import numpy as np
import matplotlib.pyplot as plt
import unittest
import torch
import torchvision
import torchvision.transforms as tvtf

%matplotlib inline
%load_ext autoreload
%autoreload 2
In [2]:
seed = 42
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
plt.rcParams.update({'font.size': 12})
test = unittest.TestCase()

Reminder: Convolutional layers and networks¶

Convolutional layers are the most essential building blocks of the state of the art deep learning image classification models and also play an important role in many other tasks. As we saw in the tutorial, when applied to images, convolutional layers operate on and produce volumes (3D tensors) of activations.

A convenient way to interpret convolutional layers for images is as a collection of 3D learnable filters, each of which operates on a small spatial region of the input volume. Each filter is convolved with the input volume ("slides over it"), and a dot product is computed at each location followed by a non-linearity which produces one activation. All these activations produce a 2D plane known as a feature map. Multiple feature maps (one for each filter) comprise the output volume.

A crucial property of convolutional layers is their translation equivariance, i.e. shifting the input results in and equivalently shifted output. This produces the ability to detect features regardless of their spatial location in the input.

Convolutional network architectures usually follow a pattern basic repeating blocks: one or more convolution layers, each followed by a non-linearity (generally ReLU) and then a pooling layer to reduce spatial dimensions. Usually, the number of convolutional filters increases the deeper they are in the network. These layers are meant to extract features from the input. Then, one or more fully-connected layers is used to combine the extracted features into the required number of output class scores.

Building convolutional networks with PyTorch¶

PyTorch provides all the basic building blocks needed for creating a convolutional arcitecture within the torch.nn package. Let's use them to create a basic convolutional network with the following architecture pattern:

[(CONV -> ACT)*P -> POOL]*(N/P) -> (FC -> ACT)*M -> FC

Here $N$ is the total number of convolutional layers, $P$ specifies how many convolutions to perform before each pooling layer and $M$ specifies the number of hidden fully-connected layers before the final output layer.

TODO: Complete the implementaion of the CNN class in the hw2/cnn.py module. Use PyTorch's nn.Conv2d and nn.MaxPool2d for the convolution and pooling layers. It's recommended to implement the missing functionality in the order of the class' methods.

In [3]:
from hw2.cnn import CNN

test_params = [
    dict(
        in_size=(3,100,100), out_classes=10,
        channels=[32]*4, pool_every=2, hidden_dims=[100]*2,
        conv_params=dict(kernel_size=3, stride=1, padding=1),
        activation_type='relu', activation_params=dict(),
        pooling_type='max', pooling_params=dict(kernel_size=2),
    ),
    dict(
        in_size=(3,100,100), out_classes=10,
        channels=[32]*4, pool_every=2, hidden_dims=[100]*2,
        conv_params=dict(kernel_size=5, stride=2, padding=3),
        activation_type='lrelu', activation_params=dict(negative_slope=0.05),
        pooling_type='avg', pooling_params=dict(kernel_size=3),
    ),
    dict(
        in_size=(3,100,100), out_classes=3,
        channels=[16]*5, pool_every=3, hidden_dims=[100]*1,
        conv_params=dict(kernel_size=2, stride=2, padding=2),
        activation_type='lrelu', activation_params=dict(negative_slope=0.1),
        pooling_type='max', pooling_params=dict(kernel_size=2),
    ),
]

for i, params in enumerate(test_params):
    torch.manual_seed(seed)
    net = CNN(**params)
    print(f"\n=== test {i=} ===")
    print(net)

    torch.manual_seed(seed)
    test_out = net(torch.ones(1, 3, 100, 100))
    print(f'{test_out=}')

    expected_out = torch.load(f'tests/assets/expected_conv_out_{i:02d}.pt')
    print(f'max_diff={torch.max(torch.abs(test_out - expected_out)).item()}')
    test.assertTrue(torch.allclose(test_out, expected_out, atol=1e-3))
=== test i=0 ===
CNN(
  (feature_extractor): Sequential(
    (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU()
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU()
    (7): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU()
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (mlp): MLP(
    (sequence): Sequential(
      (0): Linear(in_features=20000, out_features=100, bias=True)
      (1): ReLU()
      (2): Linear(in_features=100, out_features=100, bias=True)
      (3): ReLU()
      (4): Linear(in_features=100, out_features=10, bias=True)
      (5): Identity()
    )
  )
)
test_out=tensor([[ 0.0745, -0.1058,  0.0928,  0.0476,  0.0057,  0.0051,  0.0938, -0.0582,
          0.0573,  0.0583]], grad_fn=<AddmmBackward0>)
max_diff=3.725290298461914e-09

=== test i=1 ===
CNN(
  (feature_extractor): Sequential(
    (0): Conv2d(3, 32, kernel_size=(5, 5), stride=(2, 2), padding=(3, 3))
    (1): LeakyReLU(negative_slope=0.05)
    (2): Conv2d(32, 32, kernel_size=(5, 5), stride=(2, 2), padding=(3, 3))
    (3): LeakyReLU(negative_slope=0.05)
    (4): AvgPool2d(kernel_size=3, stride=3, padding=0)
    (5): Conv2d(32, 32, kernel_size=(5, 5), stride=(2, 2), padding=(3, 3))
    (6): LeakyReLU(negative_slope=0.05)
    (7): Conv2d(32, 32, kernel_size=(5, 5), stride=(2, 2), padding=(3, 3))
    (8): LeakyReLU(negative_slope=0.05)
    (9): AvgPool2d(kernel_size=3, stride=3, padding=0)
  )
  (mlp): MLP(
    (sequence): Sequential(
      (0): Linear(in_features=32, out_features=100, bias=True)
      (1): LeakyReLU(negative_slope=0.05)
      (2): Linear(in_features=100, out_features=100, bias=True)
      (3): LeakyReLU(negative_slope=0.05)
      (4): Linear(in_features=100, out_features=10, bias=True)
      (5): Identity()
    )
  )
)
test_out=tensor([[ 0.0724, -0.0030,  0.0637, -0.0073,  0.0932, -0.0662, -0.0656,  0.0076,
          0.0193,  0.0241]], grad_fn=<AddmmBackward0>)
max_diff=0.0

=== test i=2 ===
CNN(
  (feature_extractor): Sequential(
    (0): Conv2d(3, 16, kernel_size=(2, 2), stride=(2, 2), padding=(2, 2))
    (1): LeakyReLU(negative_slope=0.1)
    (2): Conv2d(16, 16, kernel_size=(2, 2), stride=(2, 2), padding=(2, 2))
    (3): LeakyReLU(negative_slope=0.1)
    (4): Conv2d(16, 16, kernel_size=(2, 2), stride=(2, 2), padding=(2, 2))
    (5): LeakyReLU(negative_slope=0.1)
    (6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (7): Conv2d(16, 16, kernel_size=(2, 2), stride=(2, 2), padding=(2, 2))
    (8): LeakyReLU(negative_slope=0.1)
    (9): Conv2d(16, 16, kernel_size=(2, 2), stride=(2, 2), padding=(2, 2))
    (10): LeakyReLU(negative_slope=0.1)
  )
  (mlp): MLP(
    (sequence): Sequential(
      (0): Linear(in_features=400, out_features=100, bias=True)
      (1): LeakyReLU(negative_slope=0.1)
      (2): Linear(in_features=100, out_features=3, bias=True)
      (3): Identity()
    )
  )
)
test_out=tensor([[-0.0004, -0.0094,  0.0817]], grad_fn=<AddmmBackward0>)
max_diff=0.0

As before, we'll wrap our model with a Classifier that provides the necessary functionality for calculating probability scores and obtaining class label predictions. This time, we'll use a simple approach that simply selects the class with the highest score.

TODO: Implement the ArgMaxClassifier in the hw2/classifier.py module.

In [4]:
from hw2.classifier import ArgMaxClassifier

model = ArgMaxClassifier(model=CNN(**test_params[0]))

test_image = torch.randint(low=0, high=256, size=(3, 100, 100), dtype=torch.float).unsqueeze(0)
test.assertEqual(model.classify(test_image).shape, (1,))
test.assertEqual(model.predict_proba(test_image).shape, (1, 10))
test.assertAlmostEqual(torch.sum(model.predict_proba(test_image)).item(), 1.0, delta=1e-3)

Let's now load CIFAR-10 to use as our dataset.

In [5]:
data_dir = os.path.expanduser('~/.pytorch-datasets')
ds_train = torchvision.datasets.CIFAR10(root=data_dir, download=True, train=True, transform=tvtf.ToTensor())
ds_test = torchvision.datasets.CIFAR10(root=data_dir, download=True, train=False, transform=tvtf.ToTensor())

print(f'Train: {len(ds_train)} samples')
print(f'Test: {len(ds_test)} samples')

x0,_ = ds_train[0]
in_size = x0.shape
num_classes = 10
print('input image size =', in_size)
Files already downloaded and verified
Files already downloaded and verified
Train: 50000 samples
Test: 10000 samples
input image size = torch.Size([3, 32, 32])

Now as usual, as a sanity test let's make sure we can overfit a tiny dataset with our model. But first we need to adapt our Trainer for PyTorch models.

TODO:

  1. Complete the implementaion of the ClassifierTrainer class in the hw2/training.py module if you haven't done so already.
  2. Set the optimizer hyperparameters in part4_optim_hp(), respectively, in hw2/answers.py.
In [6]:
from hw2.training import ClassifierTrainer
from hw2.answers import part4_optim_hp

torch.manual_seed(seed)

# Define a tiny part of the CIFAR-10 dataset to overfit it
batch_size = 2
max_batches = 25
dl_train = torch.utils.data.DataLoader(ds_train, batch_size, shuffle=False)

# Create model, loss and optimizer instances
model = ArgMaxClassifier(
    model=CNN(
        in_size, num_classes, channels=[32], pool_every=1, hidden_dims=[100],
        conv_params=dict(kernel_size=3, stride=1, padding=1),
        pooling_params=dict(kernel_size=2),
    )
)

hp_optim = part4_optim_hp()
loss_fn = hp_optim.pop('loss_fn')
optimizer = torch.optim.SGD(params=model.parameters(), **hp_optim)

# Use ClassifierTrainer to run only the training loop a few times.
trainer = ClassifierTrainer(model, loss_fn, optimizer, device)
best_acc = 0
for i in range(25):
    res = trainer.train_epoch(dl_train, max_batches=max_batches, verbose=(i%5==0))
    best_acc = res.accuracy if res.accuracy > best_acc else best_acc
    
# Test overfitting
test.assertGreaterEqual(best_acc, 90)
train_batch:   0%|          | 0/25 [00:00<?, ?it/s]
train_batch:   0%|          | 0/25 [00:00<?, ?it/s]
train_batch:   0%|          | 0/25 [00:00<?, ?it/s]
train_batch:   0%|          | 0/25 [00:00<?, ?it/s]
train_batch:   0%|          | 0/25 [00:00<?, ?it/s]

Residual Networks¶

A very common addition to the basic convolutional architecture described above are shortcut connections. First proposed by He et al. (2016), this simple addition has been shown to be a crucial ingredient in order to achieve effective learning with very deep networks. Virtually all state of the art image classification models from recent years use this technique.

The idea is to add a shortcut, or skip, around every two or more convolutional layers:

On the left we see an example of a regular Residual Block, that takes a 64 channel input, and performs two 3X3 convolutions , which are added to the original input.
On the right we see an exapmle of a Bottleneck Residual Block, that takes a 256 channel input, projects it to a 64 channel tensor with a 1X1 convolution, then performs an inner 3X3 convolution, followd by another 1X1 projection convolution back to the original numer of channels, 256. The output is then added to the original input.

Overall, we can denote the structure of the bottleneck channels in the given example as 256->64->64->256, where the first and last arrows denote the 1X1 convolutions, and the middle arrow is the inner convolution. Note that the 1X1 convolution with the default parameters (in pytorch) is defined such that the only dimension of the tensor that changes is the number of channels.

This adds an easy way for the network to learn identity mappings: set the weight values to be very small. The outcome is that the convolutional layers learn a residual mapping, i.e. some delta that is applied to the identity map, instead of actually learning a completely new mapping from scratch.

Lets start by implementing a general residual block, representing a structure similar to the above diagrams. Our residual block will be composed of:

  • A "main path" with some number of convolutional layers with ReLU between them. Optionally, we'll also apply dropout and batch normalization layers (in this order) between the convolutions, before the ReLU.
  • A "shortcut path" implementing an identity mapping around the main path. In case of a different number of input/output channels, the shortcut path should contain an additional 1x1 convolution to project the channel dimension.
  • The sum of the main and shortcut paths output is passed though a ReLU and returned.

TODO: Complete the implementation of the ResidualBlock's __init__() method in the hw2/cnn.py module.

In [7]:
from hw2.cnn import ResidualBlock

torch.manual_seed(seed)

resblock = ResidualBlock(
    in_channels=3, channels=[6, 4]*2, kernel_sizes=[3, 5]*2,
    batchnorm=True, dropout=0.2
)
print(resblock)

torch.manual_seed(seed)
test_out = resblock(torch.ones(1, 3, 32, 32))
print(f'{test_out.shape=}')

expected_out = torch.load('tests/assets/expected_resblock_out.pt')
print(f'max_diff={torch.max(torch.abs(test_out - expected_out)).item()}')
test.assertTrue(torch.allclose(test_out, expected_out, atol=1e-3))
ResidualBlock(
  (main_path): Sequential(
    (0): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): Dropout2d(p=0.2, inplace=False)
    (2): BatchNorm2d(6, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (3): ReLU()
    (4): Conv2d(6, 4, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (5): Dropout2d(p=0.2, inplace=False)
    (6): BatchNorm2d(4, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (7): ReLU()
    (8): Conv2d(4, 6, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (9): Dropout2d(p=0.2, inplace=False)
    (10): BatchNorm2d(6, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (11): ReLU()
    (12): Conv2d(6, 4, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
  )
  (shortcut_path): Conv2d(3, 4, kernel_size=(1, 1), stride=(1, 1), bias=False)
)
test_out.shape=torch.Size([1, 4, 32, 32])
max_diff=5.960464477539062e-07

Bottleneck Blocks¶

In the ResNet Block diagram shown above, the right block is called a bottleneck block. This type of block is mainly used deep in the network, where the feature space becomes increasingly high-dimensional (i.e. there are many channels).

Instead of applying a KxK conv layer on the original input channels, a bottleneck block first projects to a lower number of features (channels), applies the KxK conv on the result, and then projects back to the original feature space. Both projections are performed with 1x1 convolutions.

TODO: Complete the implementation of the ResidualBottleneckBlock in the hw2/cnn.py module.

In [8]:
from hw2.cnn import ResidualBottleneckBlock

torch.manual_seed(seed)
resblock_bn = ResidualBottleneckBlock(
    in_out_channels=256, inner_channels=[64, 32, 64], inner_kernel_sizes=[3, 5, 3],
    batchnorm=False, dropout=0.1, activation_type="lrelu"
)
print(resblock_bn)

# Test a forward pass
torch.manual_seed(seed)
test_in  = torch.ones(1, 256, 32, 32)
test_out = resblock_bn(test_in)
print(f'{test_out.shape=}')
assert test_out.shape == test_in.shape 

expected_out = torch.load('tests/assets/expected_resblock_bn_out.pt')
print(f'max_diff={torch.max(torch.abs(test_out - expected_out)).item()}')
test.assertTrue(torch.allclose(test_out, expected_out, atol=1e-3))
ResidualBottleneckBlock(
  (main_path): Sequential(
    (0): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1))
    (1): Dropout2d(p=0.1, inplace=False)
    (2): LeakyReLU(negative_slope=0.01)
    (3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (4): Dropout2d(p=0.1, inplace=False)
    (5): LeakyReLU(negative_slope=0.01)
    (6): Conv2d(64, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (7): Dropout2d(p=0.1, inplace=False)
    (8): LeakyReLU(negative_slope=0.01)
    (9): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (10): Dropout2d(p=0.1, inplace=False)
    (11): LeakyReLU(negative_slope=0.01)
    (12): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))
  )
  (shortcut_path): Identity()
)
test_out.shape=torch.Size([1, 256, 32, 32])
max_diff=1.1920928955078125e-07

Now, based on the ResidualBlock, we'll implement our own variation of a residual network (ResNet), with the following architecture:

[-> (CONV -> ACT)*P -> POOL]*(N/P) -> (FC -> ACT)*M -> FC
 \------- SKIP ------/

Note that $N$, $P$ and $M$ are as before, however now $P$ also controls the number of convolutional layers to add a skip-connection to.

TODO: Complete the implementation of the ResNet class in the hw2/cnn.py module. You must use your ResidualBlocks or ResidualBottleneckBlocks to group together every $P$ convolutional layers.

In [9]:
from hw2.cnn import ResNet

test_params = [
    dict(
        in_size=(3,100,100), out_classes=10, channels=[32, 64]*3,
        pool_every=4, hidden_dims=[100]*2,
        activation_type='lrelu', activation_params=dict(negative_slope=0.01),
        pooling_type='avg', pooling_params=dict(kernel_size=2),
        batchnorm=True, dropout=0.1,
        bottleneck=False
    ),
    dict(
        # create 64->16->64 bottlenecks
        in_size=(3,100,100), out_classes=5, channels=[64, 16, 64]*4,
        pool_every=3, hidden_dims=[64]*1,
        activation_type='tanh',
        pooling_type='max', pooling_params=dict(kernel_size=2),
        batchnorm=True, dropout=0.1,
        bottleneck=True
    )
]

for i, params in enumerate(test_params):
    torch.manual_seed(seed)
    net = ResNet(**params)
    print(f"\n=== test {i=} ===")
    print(net)

    torch.manual_seed(seed)
    test_out = net(torch.ones(1, 3, 100, 100))
    print(f'{test_out=}')
    
    expected_out = torch.load(f'tests/assets/expected_resnet_out_{i:02d}.pt')
    print(f'max_diff={torch.max(torch.abs(test_out - expected_out)).item()}')
    test.assertTrue(torch.allclose(test_out, expected_out, atol=1e-3))
=== test i=0 ===
ResNet(
  (feature_extractor): Sequential(
    (0): ResidualBlock(
      (main_path): Sequential(
        (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (1): Dropout2d(p=0.1, inplace=False)
        (2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (3): LeakyReLU(negative_slope=0.01)
        (4): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (5): Dropout2d(p=0.1, inplace=False)
        (6): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): LeakyReLU(negative_slope=0.01)
        (8): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (9): Dropout2d(p=0.1, inplace=False)
        (10): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (11): LeakyReLU(negative_slope=0.01)
        (12): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      )
      (shortcut_path): Conv2d(3, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
    )
    (1): AvgPool2d(kernel_size=2, stride=2, padding=0)
    (2): ResidualBlock(
      (main_path): Sequential(
        (0): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (1): Dropout2d(p=0.1, inplace=False)
        (2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (3): LeakyReLU(negative_slope=0.01)
        (4): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      )
      (shortcut_path): Identity()
    )
  )
  (mlp): MLP(
    (sequence): Sequential(
      (0): Linear(in_features=160000, out_features=100, bias=True)
      (1): LeakyReLU(negative_slope=0.01)
      (2): Linear(in_features=100, out_features=100, bias=True)
      (3): LeakyReLU(negative_slope=0.01)
      (4): Linear(in_features=100, out_features=10, bias=True)
      (5): Identity()
    )
  )
)
test_out=tensor([[ 0.0422,  0.0332,  0.1870, -0.0532, -0.0742,  0.1143, -0.0617, -0.0467,
          0.0852,  0.0221]], grad_fn=<AddmmBackward0>)
max_diff=8.940696716308594e-08

=== test i=1 ===
ResNet(
  (feature_extractor): Sequential(
    (0): ResidualBlock(
      (main_path): Sequential(
        (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (1): Dropout2d(p=0.1, inplace=False)
        (2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (3): Tanh()
        (4): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (5): Dropout2d(p=0.1, inplace=False)
        (6): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): Tanh()
        (8): Conv2d(16, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      )
      (shortcut_path): Conv2d(3, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
    )
    (1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (2): ResidualBottleneckBlock(
      (main_path): Sequential(
        (0): Conv2d(64, 16, kernel_size=(1, 1), stride=(1, 1))
        (1): Dropout2d(p=0.1, inplace=False)
        (2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (3): Tanh()
        (4): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (5): Dropout2d(p=0.1, inplace=False)
        (6): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): Tanh()
        (8): Conv2d(16, 64, kernel_size=(1, 1), stride=(1, 1))
      )
      (shortcut_path): Identity()
    )
    (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (4): ResidualBottleneckBlock(
      (main_path): Sequential(
        (0): Conv2d(64, 16, kernel_size=(1, 1), stride=(1, 1))
        (1): Dropout2d(p=0.1, inplace=False)
        (2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (3): Tanh()
        (4): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (5): Dropout2d(p=0.1, inplace=False)
        (6): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): Tanh()
        (8): Conv2d(16, 64, kernel_size=(1, 1), stride=(1, 1))
      )
      (shortcut_path): Identity()
    )
    (5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (6): ResidualBottleneckBlock(
      (main_path): Sequential(
        (0): Conv2d(64, 16, kernel_size=(1, 1), stride=(1, 1))
        (1): Dropout2d(p=0.1, inplace=False)
        (2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (3): Tanh()
        (4): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (5): Dropout2d(p=0.1, inplace=False)
        (6): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): Tanh()
        (8): Conv2d(16, 64, kernel_size=(1, 1), stride=(1, 1))
      )
      (shortcut_path): Identity()
    )
    (7): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (mlp): MLP(
    (sequence): Sequential(
      (0): Linear(in_features=2304, out_features=64, bias=True)
      (1): Tanh()
      (2): Linear(in_features=64, out_features=5, bias=True)
      (3): Identity()
    )
  )
)
test_out=tensor([[ 0.0237, -0.1945, -0.0085, -0.4024, -0.2667]],
       grad_fn=<AddmmBackward0>)
max_diff=2.384185791015625e-07

Questions¶

TODO Answer the following questions. Write your answers in the appropriate variables in the module hw2/answers.py.

In [10]:
from cs236781.answers import display_answer
import hw2.answers

Question 1¶

Consider the bottleneck block from the right side of the ResNet diagram above. Compare it to a regular block that performs a two 3x3 convs directly on the 256-channel input (i.e. as shown in the left side of the diagram, with a different number of channels). Explain the differences between the regular block and the bottleneck block in terms of:

  1. Number of parameters. Calculate the exact numbers for these two examples.
  2. Number of floating point operations required to compute an output (qualitative assessment).
  3. Ability to combine the input: (1) spatially (within feature maps); (2) across feature maps.
In [11]:
display_answer(hw2.answers.part4_q1)

Your answer:

  1. The number of parameters a convolution has is calculated by: $\left( K \times K \times C_{\text{in}} + 1 \right) \cdot C_{\text{out}}$.

Where $K$ is the kernel_size and $C_{\text{in}}, C_{\text{out}}$ are the number of channels, each filter also has +1 for the bias.
- The regular block have 2 convolution layers:
The first layer have $\left( 3 \times 3 \times 256 + 1 \right) \cdot 64 = 147520$ parameters.
The second layer have $\left( 3 \times 3 \times 64 + 1 \right) \cdot 256 = 147712$.
Thus, the regular block have a total of $295232$ parameters.
- The bottleneck block have 3 layers: The first have $\left( 1 \times 1 \times 256 + 1 \right) \cdot 64 = 16448$.
The second have $\left( 3 \times 3 \times 64 + 1 \right) \cdot 64 = 36928$.
The third have $\left( 1 \times 1 \times 64 + 1 \right) \cdot 256 = 16640$. Giving a total of $70016$ parameters (~25% of the regular block!).
2. The number of FLOPs done in a convolution layer is calculated by: $\# \text{FLOPs} = \left[ \left( K^2 \times C_{\text{in}} + 1 \right) \cdot C_{\text{out}} \right] \cdot \left( H \times W \right) = \# \text{parameters} \times H \times W $.
Thus, for a $32 \times 32$ images, the regular block have $302317568 \approx 300M$ FLOPs and the bottleneck block have $71696384 \approx 70M$ FLOPs.
3. Well explain the two requested aspects:
- Spatial Combination within Feature Maps: The bottleneck block will have a lower spatial ability than the regular block, because it reduces and restores the dimensions of the data to save calculations. - Combination across Feature Maps: The bottleneck reduction and restoration should not affect this ability directly. However, the spatial ability could lead to a secondary affect on this ability.

$$ \newcommand{\mat}[1]{\boldsymbol {#1}} \newcommand{\mattr}[1]{\boldsymbol {#1}^\top} \newcommand{\matinv}[1]{\boldsymbol {#1}^{-1}} \renewcommand{\vec}[1]{\boldsymbol {#1}} \newcommand{\vectr}[1]{\boldsymbol {#1}^\top} \newcommand{\rvar}[1]{\mathrm {#1}} \newcommand{\rvec}[1]{\boldsymbol{\mathrm{#1}}} \newcommand{\diag}{\mathop{\mathrm {diag}}} \renewcommand{\set}[1]{\mathbb {#1}} \newcommand{\norm}[1]{\left\lVert#1\right\rVert} \newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}} \newcommand{\bb}[1]{\boldsymbol{#1}} $$

Part 5: Convolutional Architecture Experiments¶

In this part we will explore convolution networks and the effects of their architecture on accuracy. We'll use our deep CNN implementation and perform various experiments on it while varying the architecture. Then we'll implement our own custom architecture to see whether we can get high classification results on a large subset of CIFAR-10.

Training will be performed on GPU.

In [1]:
import os
import re
import sys
import glob
import numpy as np
import matplotlib.pyplot as plt
import unittest
import torch
import torchvision
import torchvision.transforms as tvtf

%matplotlib inline
%load_ext autoreload
%autoreload 2
In [2]:
seed = 42
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
plt.rcParams.update({'font.size': 12})
test = unittest.TestCase()

Experimenting with model architectures¶

We will now perform a series of experiments that train various model configurations on a part of the CIFAR-10 dataset.

To perform the experiments, you'll need to use a machine with a GPU since training time might be too long otherwise.

Note about running on GPUs¶

Here's an example of running a forward pass on the GPU (assuming you're running this notebook on a GPU-enabled machine).

In [3]:
from hw2.cnn import ResNet

net = ResNet(
    in_size=(3,100,100), out_classes=10, channels=[32, 64]*3,
    pool_every=4, hidden_dims=[100]*2,
    pooling_type='avg', pooling_params=dict(kernel_size=2),
)
net = net.to(device)

test_image = torch.randint(low=0, high=256, size=(3, 100, 100), dtype=torch.float).unsqueeze(0)
test_image = test_image.to(device)

test_out = net(test_image)

Notice how we called .to(device) on both the model and the input tensor. Here the device is a torch.device object that we created above. If an nvidia GPU is available on the machine you're running this on, the device will be 'cuda'. When you run .to(device) on a model, it recursively goes over all the model parameter tensors and copies their memory to the GPU. Similarly, calling .to(device) on the input image also copies it.

In order to train on a GPU, you need to make sure to move all your tensors to it. You'll get errors if you try to mix CPU and GPU tensors in a computation.

In [4]:
print(f'This notebook is running with device={device}')
print(f'The model parameter tensors are also on device={next(net.parameters()).device}')
print(f'The test image is also on device={test_image.device}')
print(f'The output is therefore also on device={test_out.device}')
This notebook is running with device=cpu
The model parameter tensors are also on device=cpu
The test image is also on device=cpu
The output is therefore also on device=cpu

Notes on using course servers¶

First, please read the course servers guide carefully.

To run the experiments on the course servers, you can use the py-sbatch.sh script directly to perform a single experiment run in batch mode (since it runs python once), or use the srun command to do a single run in interactive mode. For example, running a single run of experiment 1 interactively (after conda activate of course):

srun -c 2 --gres=gpu:1 --pty python -m hw2.experiments run-exp -n test -K 32 64 -L 2 -P 2 -H 100

To perform multiple runs in batch mode with sbatch (e.g. for running all the configurations of an experiments), you can create your own script based on py-sbatch.sh and invoke whatever commands you need within it.

Don't request more than 2 CPU cores and 1 GPU device for your runs. The code won't be able to utilize more than that anyway, so you'll see no performance gain if you do. It will only cause delays for other students using the servers.

General notes for running experiments¶

  • You can run the experiments on a different machine (e.g. the course servers) and copy the results (files) to the results folder on your local machine. This notebook will only display the results, not run the actual experiment code (except for a demo run).
  • It's important to give each experiment run a name as specified by the notebook instructions later on. Each run has a run_name parameter that will also be the base name of the results file which this notebook will expect to load.
  • You will implement the code to run the experiments in the hw2/experiments.py module. This module has a CLI parser so that you can invoke it as a script and pass in all the configuration parameters for a single experiment run.
  • You should use python -m hw2.experiments run-exp to run an experiment, and not python hw2/experiments.py run-exp, regardless of how/where you run it.

Experiment 1: Network depth and number of filters¶

In this part we will test some different architecture configurations based on our CNN and ResNet. Specifically, we want to try different depths and number of features to see the effects these parameters have on the model's performance.

To do this, we'll define two extra hyperparameters for our model, K (filters_per_layer) and L (layers_per_block).

  • K is a list, containing the number of filters we want to have in our conv layers.
  • L is the number of consecutive layers with the same number of filters to use.

For example, if K=[32, 64] and L=2 it means we want two conv layers with 32 filters followed by two conv layers with 64 filters. If we also use pool_every=3, the feature-extraction part of our model will be:

Conv(X,32)->ReLu->Conv(32,32)->ReLU->Conv(32,64)->ReLU->MaxPool->Conv(64,64)->ReLU

We'll try various values of the K and L parameters in combination and see how each architecture trains. All other hyperparameters are up to you, including the choice of the optimization algorithm, the learning rate, regularization and architecture hyperparams such as pool_every and hidden_dims. Note that you should select the pool_every parameter wisely per experiment so that you don't end up with zero-width feature maps.

You can try some short manual runs to determine some good values for the hyperparameters or implement cross-validation to do it. However, the dataset size you test on should be large. If you limit the number of batches, make sure to use at least 30000 training images and 5000 validation images.

The important thing is that you state what you used, how you decided on it, and explain your results based on that.

First we need to write some code to run the experiment.

TODO:

  1. Implement the cnn_experiment() function in the hw2/experiments.py module.
  2. If you haven't done so already, it would be an excellent idea to implement the early stopping feature of the Trainer class.

The following block tests that your implementation works. It's also meant to show you that each experiment run creates a result file containing the parameters to reproduce and the FitResult object for plotting.

In [5]:
from hw2.experiments import load_experiment, cnn_experiment
from cs236781.plot import plot_fit
In [6]:
# Test experiment1 implementation on a few data samples and with a small model
cnn_experiment(
    'test_run', seed=seed, bs_train=50, batches=10, epochs=10, early_stopping=5,
    filters_per_layer=[32,64], layers_per_block=1, pool_every=1, hidden_dims=[100],
    model_type='resnet',
)

# There should now be a file 'test_run.json' in your `results/` folder.
# We can use it to load the results of the experiment.
cfg, fit_res = load_experiment('results/test_run_L1_K32-64.json')
_, _ = plot_fit(fit_res, train_test_overlay=True)

# And `cfg` contains the exact parameters to reproduce it
print('experiment config: ', cfg)
Files already downloaded and verified
Files already downloaded and verified
--- EPOCH 1/10 ---
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
--- EPOCH 10/10 ---
train_batch:   0%|          | 0/1000 [00:00<?, ?it/s]
test_batch:   0%|          | 0/834 [00:00<?, ?it/s]
*** Output file ./results/test_run_L1_K32-64.json written
experiment config:  {'run_name': 'test_run', 'out_dir': './results', 'seed': 42, 'device': None, 'bs_train': 50, 'bs_test': 12, 'batches': 10, 'epochs': 10, 'early_stopping': 5, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'filters_per_layer': [32, 64], 'pool_every': 1, 'hidden_dims': [100], 'model_type': 'resnet', 'kw': {'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_type': 'max', 'pooling_params': {'kernel_size': 2, 'stride': 2}}, 'layers_per_block': 1}

We'll use the following function to load multiple experiment results and plot them together.

In [7]:
def plot_exp_results(filename_pattern, results_dir='results'):
    fig = None
    result_files = glob.glob(os.path.join(results_dir, filename_pattern))
    result_files.sort()
    if len(result_files) == 0:
        print(f'No results found for pattern {filename_pattern}.', file=sys.stderr)
        return
    for filepath in result_files:
        m = re.match('exp\d_(\d_)?(.*)\.json', os.path.basename(filepath))
        cfg, fit_res = load_experiment(filepath)
        fig, axes = plot_fit(fit_res, fig, legend=m[2],log_loss=True)
    del cfg['filters_per_layer']
    del cfg['layers_per_block']
    print('common config: ', cfg)

Experiment 1.1: Varying the network depth (L)¶

First, we'll test the effect of the network depth on training.

Configuratons:

  • K=32 fixed, with L=2,4,8,16 varying per run
  • K=64 fixed, with L=2,4,8,16 varying per run

So 8 different runs in total.

Naming runs: Each run should be named exp1_1_L{}_K{} where the braces are placeholders for the values. For example, the first run should be named exp1_1_L2_K32.

TODO: Run the experiment on the above configuration with the CNN model. Make sure the result file names are as expected. Use the following blocks to display the results.

In [8]:
plot_exp_results('exp1_1_L*_K32*.json')
common config:  {'run_name': 'exp1_1', 'out_dir': './results', 'seed': 1193202414, 'device': None, 'bs_train': 128, 'bs_test': 32, 'batches': 100, 'epochs': 60, 'early_stopping': 3, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'pool_every': 5, 'hidden_dims': [1024], 'model_type': 'cnn', 'kw': {'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_type': 'max', 'pooling_params': {'kernel_size': 2, 'stride': 2}}}
In [9]:
plot_exp_results('exp1_1_L*_K64*.json')
common config:  {'run_name': 'exp1_1', 'out_dir': './results', 'seed': 1952788699, 'device': None, 'bs_train': 128, 'bs_test': 32, 'batches': 100, 'epochs': 60, 'early_stopping': 3, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'pool_every': 5, 'hidden_dims': [1024], 'model_type': 'cnn', 'kw': {'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_type': 'max', 'pooling_params': {'kernel_size': 2, 'stride': 2}}}

Experiment 1.2: Varying the number of filters per layer (K)¶

Now we'll test the effect of the number of convolutional filters in each layer.

Configuratons:

  • L=2 fixed, with K=[32],[64],[128] varying per run.
  • L=4 fixed, with K=[32],[64],[128] varying per run.
  • L=8 fixed, with K=[32],[64],[128] varying per run.

So 9 different runs in total. To clarify, each run K takes the value of a list with a single element.

Naming runs: Each run should be named exp1_2_L{}_K{} where the braces are placeholders for the values. For example, the first run should be named exp1_2_L2_K32.

TODO: Run the experiment on the above configuration with the CNN model. Make sure the result file names are as expected. Use the following blocks to display the results.

In [10]:
plot_exp_results('exp1_2_L2*.json')
common config:  {'run_name': 'exp1_2', 'out_dir': './results', 'seed': 1964681515, 'device': None, 'bs_train': 128, 'bs_test': 32, 'batches': 100, 'epochs': 60, 'early_stopping': 3, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'pool_every': 2, 'hidden_dims': [1024], 'model_type': 'cnn', 'kw': {'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_type': 'max', 'pooling_params': {'kernel_size': 2, 'stride': 2}}}
In [11]:
plot_exp_results('exp1_2_L4*.json')
common config:  {'run_name': 'exp1_2', 'out_dir': './results', 'seed': 1593737681, 'device': None, 'bs_train': 128, 'bs_test': 32, 'batches': 100, 'epochs': 60, 'early_stopping': 3, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'pool_every': 3, 'hidden_dims': [1024], 'model_type': 'cnn', 'kw': {'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_type': 'max', 'pooling_params': {'kernel_size': 2, 'stride': 2}}}
In [12]:
plot_exp_results('exp1_2_L8*.json')
common config:  {'run_name': 'exp1_2', 'out_dir': './results', 'seed': 1292644496, 'device': None, 'bs_train': 128, 'bs_test': 32, 'batches': 100, 'epochs': 60, 'early_stopping': 3, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'pool_every': 5, 'hidden_dims': [1024], 'model_type': 'cnn', 'kw': {'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_type': 'max', 'pooling_params': {'kernel_size': 2, 'stride': 2}}}

Experiment 1.3: Varying both the number of filters (K) and network depth (L)¶

Now we'll test the effect of the number of convolutional filters in each layer.

Configuratons:

  • K=[64, 128] fixed with L=2,3,4 varying per run.

So 3 different runs in total. To clarify, each run K takes the value of an array with a two elements.

Naming runs: Each run should be named exp1_3_L{}_K{}-{} where the braces are placeholders for the values. For example, the first run should be named exp1_3_L2_K64-128.

TODO: Run the experiment on the above configuration with the CNN model. Make sure the result file names are as expected. Use the following blocks to display the results.

In [13]:
plot_exp_results('exp1_3*.json')
common config:  {'run_name': 'exp1_3', 'out_dir': './results', 'seed': 1809054485, 'device': None, 'bs_train': 128, 'bs_test': 32, 'batches': 100, 'epochs': 100, 'early_stopping': 3, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'pool_every': 4, 'hidden_dims': [1024], 'model_type': 'cnn', 'kw': {'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_type': 'max', 'pooling_params': {'kernel_size': 2, 'stride': 2}}}

Experiment 1.4: Adding depth with Residual Networks¶

Now we'll test the effect of skip connections on the training and performance.

Configuratons:

  • K=[32] fixed with L=8,16,32 varying per run.
  • K=[64, 128, 256] fixed with L=2,4,8 varying per run.

So 6 different runs in total.

Naming runs: Each run should be named exp1_4_L{}_K{}-{}-{} where the braces are placeholders for the values.

TODO: Run the experiment on the above configuration with the ResNet model. Make sure the result file names are as expected. Use the following blocks to display the results.

In [14]:
plot_exp_results('exp1_4_L*_K32.json')
common config:  {'run_name': 'exp1_4_NoDrop', 'out_dir': './results', 'seed': 1008925868, 'device': None, 'bs_train': 128, 'bs_test': 32, 'batches': 100, 'epochs': 60, 'early_stopping': 3, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'pool_every': 5, 'hidden_dims': [1024], 'model_type': 'resnet', 'kw': {'batchnorm': True, 'pooling_type': 'avg', 'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_params': {'kernel_size': 2, 'stride': 2}}}
In [15]:
plot_exp_results('exp1_4_L*_K64*.json')
common config:  {'run_name': 'exp1_4_NoDrop', 'out_dir': './results', 'seed': 1293339002, 'device': None, 'bs_train': 128, 'bs_test': 32, 'batches': 100, 'epochs': 60, 'early_stopping': 3, 'checkpoints': None, 'lr': 0.001, 'reg': 0.001, 'pool_every': 5, 'hidden_dims': [1024], 'model_type': 'resnet', 'kw': {'batchnorm': True, 'pooling_type': 'avg', 'conv_params': {'kernel_size': 3, 'padding': 1, 'stride': 1}, 'activation_type': 'relu', 'activation_params': {}, 'pooling_params': {'kernel_size': 2, 'stride': 2}}}

Questions¶

TODO Answer the following questions. Write your answers in the appropriate variables in the module hw2/answers.py.

In [16]:
from cs236781.answers import display_answer
import hw2.answers

Question 1¶

Analyze your results from experiment 1.1. In particular,

  1. Explain the effect of depth on the accuracy. What depth produces the best results and why do you think that's the case?
  2. Were there values of L for which the network wasn't trainable? what causes this? Suggest two things which may be done to resolve it at least partially.
In [17]:
display_answer(hw2.answers.part5_q1)

Your answer:

Question 1¶

Analyze your results from experiment 1.1. In particular,

  1. More depth doesn't mean better accuracy

We can see that the lower depths L=2 and L=4 performed better than L=8 and L=16. For K=32 the depths L=8,16 were untrainable and for K=64 the depth L=16 was untrainable. This shows than increasing the depth helps until some upper bound, then after that the performance begin to decrease.

  1. We can see that both L=8 and L=16 were untrainable.

A probable cause for that is the vanishing/exploding gradients effect that usually happens in deep networks.
A solution to that problem can be using a different activation function, using batch normalization or initializing the weights differently.

Question 2¶

Analyze your results from experiment 1.2. In particular, compare to the results of experiment 1.1.

In [18]:
display_answer(hw2.answers.part5_q2)

Your answer:

We can see that again, more width doesn't mean better accuracy.

  • For L=4 the higher K gave us better performance (by a little), but on the other hand for L=2, the best performing K was K=32.

For L=2 the results were very close, while for L=8 only the K=64 model was trainable.

  • Both experiment gave us a similar conclusion, there is a trade-off between high and low depth/width, and we should look for the middle "sweet spot".

Question 3¶

Analyze your results from experiment 1.3.

In [19]:
display_answer(hw2.answers.part5_q3)

Your answer:

  • We can see that the varying number of filters K made the model a little better in terms of accuracy.
  • The model with L=4 was untrainable, probably due to the same vanishing/exploding gradients problem.
  • This experiment shows us that increasing K throughout the depth of the network, helps increasing the model's performance.
  • This gives us a good intuition for a good architecture for these CNNs.

Question 4¶

Analyze your results from experiment 1.4. Compare to experiment 1.1 and 1.3.

In [20]:
display_answer(hw2.answers.part5_q4)

Your answer:

  • First of all, We can see that the ResNet model gave us much higher score compared to the regular CNN model.
  • With L=2 and K=[64, 128, 256] we got the best results - around 90%.

This is consistent with the idea of increasing the number of channels the deeper we go

  • In comparrison to experiments 1.1 and 1.3, the ResNet models were much more resilient to the higher depth of the network.

We saw that the regular CNN models failed to train with a high L value, but the ResNet models were able to perform very well with them.

$$ \newcommand{\mat}[1]{\boldsymbol {#1}} \newcommand{\mattr}[1]{\boldsymbol {#1}^\top} \newcommand{\matinv}[1]{\boldsymbol {#1}^{-1}} \renewcommand{\vec}[1]{\boldsymbol {#1}} \newcommand{\vectr}[1]{\boldsymbol {#1}^\top} \newcommand{\rvar}[1]{\mathrm {#1}} \newcommand{\rvec}[1]{\boldsymbol{\mathrm{#1}}} \newcommand{\diag}{\mathop{\mathrm {diag}}} \renewcommand{\set}[1]{\mathbb {#1}} \newcommand{\norm}[1]{\left\lVert#1\right\rVert} \newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}} \newcommand{\bb}[1]{\boldsymbol{#1}} $$

Part 6: YOLO - Objects Detection¶

In this part we will use an object detection architecture called YOLO (You only look once) to detect objects in images. We'll use an already trained model weights (v5) found here: https://github.com/ultralytics/yolov5

In [1]:
import torch

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Load the YOLO model
model = torch.hub.load("ultralytics/yolov5", "yolov5s")
model.to(device)
# Images
img1 = 'imgs/DolphinsInTheSky.jpg'  
img2 = 'imgs/cat-shiba-inu-2.jpg' 
Using cache found in /home/ofer.nissim/.cache/torch/hub/ultralytics_yolov5_master
YOLOv5 🚀 2023-5-25 Python-3.8.12 torch-1.10.1 CPU

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
Adding AutoShape... 

Inference with YOLO¶

You are provided with 2 images (img1 and img2). TODO:

  1. Detect objects using the YOLOv5 model for these 2 images.

  2. Print the inference output with bounding boxes.

  3. Calculate the number of pixels within a bounding box and the number in the background.

    Hint: Given you stored the model output in a varibale named 'results', you may find 'results.pandas().xyxy' helpful

  4. Look at the inference results and answer the question below.

In [2]:
#Insert the inference code here.
import cv2

from shapely.geometry import Polygon
from shapely.geometry import box
from shapely.ops import unary_union


def get_boxes_unified_area(results):
    polygons = []
    objects = results.pandas().xyxy[0]
    
    for current_box in range(len(objects)):
        boundaries_coordinates = objects.iloc[current_box][:4]
        polygons.append(Polygon(box(*boundaries_coordinates)))
        
    # unify all the polygons boxes and get the unified area
    return unary_union(polygons).area 

def results_show(im_path):
    results_show.i += 1
    # Inference
    results = model(im_path)
    
    # Get number of pixels within a bounding box
    num_pixels_bounded_box = get_boxes_unified_area(results)
    
    # Get number of pixels in the background
    img = cv2.imread(im_path)
    num_pixels_total = img.shape[0] * img.shape[1]
    
    # show the results
    print("\033[1m" +f"Results for image {results_show.i}:")
    results.show()
    print(results.pandas().xyxy[0])
    print(f"\n- Number of Bounded Pixels:  {num_pixels_bounded_box:.0f}\n- Number of Background Pixels: {(num_pixels_total - num_pixels_bounded_box):.0f}\n\n") 


results_show.i = 0
results_show(img1)
results_show(img2)
Results for image 1:
         xmin       ymin        xmax        ymax  confidence  class       name
0  100.278381  47.370995  187.863556  118.462662    0.903491      0     person
1   22.531363  20.887983  128.905212   92.257050    0.500973      0     person
2   85.617989  98.014702  139.018356  124.627922    0.367016     37  surfboard

- Number of Bounded Pixels:  13162
- Number of Background Pixels: 37163


Results for image 2:
         xmin        ymin        xmax        ymax  confidence  class name
0   11.568563  115.774918  312.757080  667.323730    0.655977     15  cat
1  363.715485  290.178558  750.000000  721.983826    0.509416     16  dog
2  311.190765  102.437584  595.031799  687.870605    0.391701     15  cat

- Number of Bounded Pixels:  406233
- Number of Background Pixels: 156267


Question 1¶

Analyze the inference results of the 2 images.

  1. How well did the model detect the objects in the pictures?
  2. What can possibly be the reason for the model failures? suggest methods to resolve that issue.
In [3]:
from cs236781.answers import display_answer
import hw2.answers
In [4]:
display_answer(hw2.answers.part6_q1)

Your answer:

  1. The model managed to detect the boundaries of the objects in the pictures generally well, but its classifications were mostly wrong.

It can be seen in the first picture, where two dolphins were classified as persons and the tail of the third one was classified as a surfboard. In the second picture, two dogs were classified as cats and the cat was not detected at all. We'll also notice that the dog that was classified correctly had a score of 0.51, which implies a significant level of uncertainty. All in all, the model's performance was not good for these 2 pictures.
2. We'll examine numerous reasons for the model failures and suggest compatible solutions for the issues:

  • A possible reason for the model failure is lack of light/dark images, which makes objects' features less noticeable and produces difficulties in the models classification.

This can be resolved by adding brightness to the images, changing their contrast rate or using similar lighting techniques.

  • Another possible reason for the model failures can be a missing class, on which the model was not trained - like in the first picture, where it's seems like there is no dolphin class exists.

The solution for that issue is to train the model to classify dolphins in addition to the existing classes.

  • Furthermore, a bias that is related to image setup in the training phase could lead to a misclassification of objects.

For example, in the first image there is a background of sea surface, sun and sky, which might cause the model to classify the dolphins as humans - in case it was trained on pictures of humans with beach background and on pictures of dolphins with underwater background. To solve this problem, more pictures of each class can be provided for the training, containing different backgrounds in order to prevent this kind of classification bias.

  • Looking at the second picture, the overlapping of the objects (occlusion) might cause misclassification or missing detection of an object.

A solution for that might involve some spatial manipulations like rotation, cropping or resizing some objects in the pictures. We'll mention that the YOLO model's algorithm divides the image into a grid of boxes, and tries to detect+classify an object in each box according to its center. Adjusting the number of boxes in the grid (equivalent to changing the boxes' sizes) could improve the model's performance in this situation.

Creative Detection Failures¶

Object detection pitfalls could be, for example: occlusion - when the objects are partially occlude, and thus missing important features, model bias - when a model learn some bias about an object, it could recognize it as something else in a different setup, and many others like Deformation, Illumination conditions, Cluttered or textured background and blurring due to moving objects.

TODO: Take pictures and that demonstrates 3 of the above object detection pitfalls, run inference and analyze the results.

In [5]:
#Insert the inference code here.

# Images
img1 = 'taken_imgs/occlusion-kid.jpg'  
img2 = 'taken_imgs/bias-cellphone.jpg' 
img3 = 'taken_imgs/blurry-dog.jpg'

# show the results
print('Demonstrating occlusion:')
results = model(img1)
results.show()
print(results.pandas().xyxy[0])

print('\nDemonstrating model bias:')
results = model(img2)
results.show()
print(results.pandas().xyxy[0])

print('\nDemonstrating blurring:')
results = model(img3)
results.show()
print(results.pandas().xyxy[0])
Demonstrating occlusion:
         xmin      ymin       xmax       ymax  confidence  class  name
0  292.150452  51.38649  392.23999  221.99379    0.290816     14  bird

Demonstrating model bias:
         xmin         ymin         xmax         ymax  confidence  class   
0  445.193329  1251.814087  1430.635254  1792.678101    0.880508     67  \
1  342.662903    93.861008   815.978271  1101.983398    0.394383     65   

         name  
0  cell phone  
1      remote  

Demonstrating blurring:
        xmin       ymin        xmax        ymax  confidence  class   name
0  93.195099  56.552513  830.170715  425.511475    0.768481     17  horse

Question 3¶

Analyize the results of the inference.

  1. How well did the model detect the objects in the pictures? explain.
In [6]:
display_answer(hw2.answers.part6_q3)

Your answer:

The model detected the objects in the pictures very badly (again the detection of boundaries was good, but a total misclassification was demonstrated):

  • In the occlusion demonstration picture, a child's body was hidden behind a tree, such that only her head was appearing.

The child was classified as a bird instead of a person, but with a relatively low confidence level - 0.29.

  • In the model bias demonstration picture, a cell phone was caught in different views (front, side, back and angular).

The front and the side views were not detected at all, whereas the back view was classified as a remote. Only the angular view was classified correctly (with high confidence level!).

  • In the blurring demonstration picture, a running dog was caught without a proper focus - thus the obtained picture was blurred.

The dog was classified as a horse with a high confidence level - 0.77.

These all imply some limitations in the model's abilities, leading to poor performance in the given situations.

Bonus¶

Try improving the model performance over poorly recognized images by changing them. Describe the manipulations you did to the pictures.

In [7]:
#Insert the inference code here.

# Images
img1 = 'taken_imgs/occlusion-kid-fix.jpg'  
img1_1 = 'taken_imgs/occlusion-kid-fix2.jpg'  
img2 = 'taken_imgs/bias-cellphone-fix.jpg' 
img3 = 'taken_imgs/blurry-dog-fix.jpg'

# show the fixes results
print('Trying to fix occlusion:')
results = model(img1)
results.show()
print(results.pandas().xyxy[0])

print('\nTrying again to fix occlusion:')
results = model(img1_1)
results.show()
print(results.pandas().xyxy[0])

print('\nTrying to fix model bias:')
results = model(img2)
results.show()
print(results.pandas().xyxy[0])

print('\nTrying to fix blurring:')
results = model(img3)
results.show()
print(results.pandas().xyxy[0])
Trying to fix occlusion:
   xmin       ymin       xmax        ymax  confidence  class name
0   0.0  63.162506  96.066895  212.445648    0.250114     16  dog

Trying again to fix occlusion:
         xmin        ymin        xmax        ymax  confidence  class    name
0   52.833168  244.141891  166.887955  340.508942    0.499462      0  person
1  186.226654  326.784576  208.420319  340.992157    0.258678      0  person

Trying to fix model bias:
         xmin         ymin         xmax         ymax  confidence  class   
0  443.160492  1252.394531  1429.164673  1791.688843    0.876003     67  \
1   10.093597    32.694489   908.030273  1065.914673    0.421801     67   

         name  
0  cell phone  
1  cell phone  

Trying to fix blurring:
         xmin       ymin        xmax        ymax  confidence  class    name
0  106.199364  64.852112  834.129150  449.548370    0.680178     17   horse
1  118.411469  56.951603  263.671906  366.091095    0.474996      0  person
In [8]:
display_answer(hw2.answers.part6_bonus)

Your answer:

Trying to improve the model's performance on the previous pictures, we made the following manipulations:

  • In the occlusion demonstration picture, At first we cropped the tree from the image - yielding another misclassification of the child - dog class with confidence level of 0.25.

The change of classification indicated that the occlusion with the tree had some influence on the detection. We assumed that in addition to the occlusion, model bias is involved due to the rotated face of the child (parallel to the ground), so in order to fix that - we now also performed a counter-rotation to the image, resulted with a correct classification of the child - person class with confidence level of 0.50.

  • In the model bias demonstration picture, Seeing that only the angular view was classified correctly - We rotated by 45 degrees the front and the back views of the cell phones.

The result was a correct classification of the back view - cell phone class with confidence level of 0.42 (which was a bit low for what we expected). Unfortunately, the rotated front view was still not detected at all, a result that is probably related to training bias - where images of cell phones were taken mainly on angular views (not only rotated).

  • In the blurring demonstration picture, we tried to sharpen the image and clarify it, using some common filters.

The result was still noisy and blurred, though it became a bit clearer, hence the model again misclassified it - this time it detected two objects, horse (same box as earlier, this time with lower confidence level of 0.68) and a person (over the dog's tail area). From what we understand, the sharpening manipulation might had amplified some noise in this region of the image, which became now more noticable and mislead the model to detect another object.

In summery, the manipulations we've performed indeed improved the model's performance over the poorly recognized images. It involved some spatial manipulations like rotation and cropping, along with fine-tuning filters like the sharpening, resulting in a correct classification for the majority of the images. It is worth to mention that for some situations (like the blurred dog image), a more sophisticated manipulation is required to correctly classify the required objects.